官网大法好:
点击直达官网
参考:
我的:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
labels = ['C3', 'C4', 'C5', 'C6']
im_ip = [20, 34, 30, 35]
cg_ip = [25, 32, 34, 20]
cg_lp = [2, 3, 4, 2]
x = np.arange(len(labels)) # the label locations
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
#x操作水平位置
rects1 = ax.bar(x - width, im_ip, width,color='cornflowerblue', label='M1 IP')
rects2 = ax.bar(x, cg_ip, width, color='orange',label='M2 IP')
#bottom操作堆叠位置
rects3 = ax.bar(x, cg_lp, width,bottom=cg_ip,color='yellow', label='M2 LP')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Runtime (s)')
#ax.set_title('Scores by group and gender')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend(loc='upper left')
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
#autolabel(rects1)
#autolabel(rects2)
fig.tight_layout()
plt.savefig('demo.png')#保存
plt.show()
效果