import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(8,5))
size = 9
x = np.arange(size)
data1=np.array([7, 6, 6, 6.5, 5, 5, 8, 5, 7])
data2=np.array([8, 7, 5, 3, 6, 4, 7, 4, 8])
#创建数据
total_width,n=0.8,2
width = total_width/n
x=x-(total_width - width)/2
plt.bar(x,data1,color = 'darkorange', width=width,label='data1')#绘制纵向条形图 x表示开始绘图的位置
plt.bar(x+width,data2,color = 'steelblue',width=width,label='data2')#x+width挨着上一个绘图
for a,b in zip (x,data1):#在条形图的上方标记数值
plt.text(a,b,"%.0f"%b,ha="center",va="bottom")
for a,b in zip (x+width,data2):
plt.text(a,b,"%.0f"%b,ha="center",va="bottom")
plt.legend()
plt.show()
<