python绘制柱状图形+柱状图增加数字标注
data = pd.Series([4, 5, 6], index=['A','B','C'])
fig = plt.figure(figsize=(7, 5), dpi=90) # 声明画布1
ax = fig.add_subplot(1,1,1) # 声明绘图区
x, y = data.index, data.values
rects = plt.bar(x, y, color='dodgerblue', width=0.35, label='label1')
plt.grid(linestyle="-.", axis='y', alpha=0.4)
plt.legend()
plt.tight_layout()
注:line= plt.bar(label=‘label’) # 这里的label参数 表示当前 x,y 图形的图例名称
方法一:
for rect in rects: #rects 是三根柱子的集合
height = rect.get_height()
plt.text(rect.get_x() + rect.get_width() / 2, height, str(height), size=15, ha='center', va='bottom')
方法二:
for a,b in zip(x,y):
plt.text(a, b-0.3,'%.3f'%b, ha = 'center',va = 'bottom',fontsize=15)
plt.show()
plt.text(相关参数)
plt.text(x,y,str) #x,y :位置坐标; str :标注内容