([<matplotlib.axis.XTick at 0x242ff375b38>,
<matplotlib.axis.XTick at 0x242ff375400>,
<matplotlib.axis.XTick at 0x242ff3752e8>],
<a list of 3 Text xticklabel objects>)
背靠背画法
x1 = np.array([1,2,3])
x2 = np.array([2,2,3])
bar_labels =['bar1','bar2','bar3']# 尺寸
fig = plt.figure(figsize=(8,4))
y_pos = np.arange(len(x1))
y_pos =[x for x in y_pos]
plt.barh(y_pos,x1,color='g')
plt.barh(y_pos,-x1,color='r')
<BarContainer object of 3 artists>
多个柱子
green_data =[1,2,3]
blue_data =[3,2,2]
red_data =[1,3,3]
labels =['g1','g2','g3']
pos =list(range(len(green_data)))
width =0.2
fig, ax = plt.subplots(figsize=(8,6))# pos其实就是x坐标,width是柱子宽度,所以可以理解了吧
plt.bar(pos,green_data,width,color='g',label=labels[0])
plt.bar([p+width for p in pos],blue_data,width,color='b',label=labels[1])
plt.bar([p+width*2for p in pos],red_data,width,color='r',label=labels[2])
<BarContainer object of 3 artists>
给柱子加上标
green_data =[1,2,3]
blue_data =[3,2,2]
red_data =[1,3,3]
labels =['g1','g2','g3']
pos =list(range(len(green_data)))
width =0.2
fig, ax = plt.subplots(figsize=(8,6))# pos其实就是x坐标,width是柱子宽度,所以可以理解了吧
plt.bar(pos,green_data,width,color='g',label=labels[0])
plt.bar([p+width for p in pos],blue_data,width,color='b',label=labels[1])
bars = plt.bar([p+width*2for p in pos],red_data,width,color='r',label=labels[2])
plt.ylim([0,3.5])# 加上标for b,d inzip(bars, red_data):
plt.text(b.get_x()+b.get_width()*0.05, b.get_y()+b.get_height()*1.04,'{0:.2%}'.format(d/min(red_data)))
颜色
mean_values =range(10,18)
x_pos =range(len(mean_values))import matplotlib.colors as col
import matplotlib.cm as cm
camp1 = cm.ScalarMappable(col.Normalize(min(mean_values),max(mean_values),cm.hot))
camp2 = cm.ScalarMappable(col.Normalize(0,20,cm.hot))
plt.subplot(121)
plt.bar(x_pos,mean_values,color=camp1.to_rgba(mean_values))