1 error bar
#!/usr/bin/env python
# a bar plot with errorbars
import numpy as np
import matplotlib.pyplot as plt N = 5
menMeans = (20, 35, 30, 35, 27)
menStd = (2, 3, 4, 1, 2) ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd) womenMeans = (25, 32, 34, 20, 25)
womenStd = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, womenMeans, width, color='y', yerr=womenStd) # add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) ax.legend((rects1[0], rects2[0]), ('Men', 'Women')) def autolabel(rects):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
'%d' % int(height),
ha='center', va='bottom') autolabel(rects1)
autolabel(rects2) plt.show()
2 绘制两个量,一个在上,一个在下.关键点在于
p2 = plt.bar(ind, OECD, width, color = 'b', bottom = NonOECD) 这个bottom参数
import numpy as np
import matplotlib.pyplot as plt N = 7
OECD = (242, 244, 255, 263, 269, 276, 285)
NonOECD = (282, 328, 375, 417, 460, 501, 535)
Sum = ('', '', '', '', '', '', '')
ind = np.arange(N)
width = 0.5 p1 = plt.bar(ind, NonOECD, width, color = 'r')
p2 = plt.bar(ind, OECD, width, color = 'b', bottom = NonOECD) plt.ylabel('Quadrillion Btu')
plt.title('World Total Energy Consumption 2010 - 2040')
plt.xticks(ind+width/2., ('', '', '', '', '', '', ''))
plt.yticks(np.arange(0, 1001, 200))
plt.legend((p1[0], p2[0]), ('Non - OECD', 'OECD'), loc = 2, frameon = 'false')
plt.tick_params(top = 'off', bottom = 'off', right = 'off')
plt.grid(axis = 'y', linestyle = '-') plt.show()
3 默认的x轴刻度是[1,...],而且一般会缩略,可是我们希望x轴刻度是x轴数据本身的范围。
如果这样
axes.bar(bin_index, num_list, bar_width, color='r')
axes.set_xticks(xticks)
是得不到我们想要的,因为在缩略后,刻度可能只有4个,而set_xticks时,就变成只有这四个刻度上有值,而且这些值不是想象中得自动缩略得到,而是只有xticks这个列表中前四个,这样就与我们的要求不符合