matplotlib条形图
程序员文章站
2022-04-21 15:02:34
...
from matplotlib import pyplot as plt
import numpy as np
fig=plt.figure()
ax=fig.add_subplot(111)
x=np.arange(4)
data=np.array([15,20,18,25])
rect=ax.bar(x,data,width=0.5,color="lightblue")
for rec in rect:#添加数值
x=rec.get_x()
height=rec.get_height()
ax.text(x+0.1,1.02*height,str(height))
ax.set_xticks([0,1,2,3])
ax.set_xticklabels(("first","second","third","fourth"))
ax.set_ylabel("sales")
ax.set_title("The Sales in 2016")
ax.grid(True)
ax.set_ylim(0,28)