搜索
您的当前位置:首页正文

python绘制柱状图形+柱状图增加数字标注

来源:欧得旅游网

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()

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注: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')

   
   
  • 1
  • 2
  • 3

方法二:

for a,b in zip(x,y):
plt.text(a, b-0.3,'%.3f'%b, ha = 'center',va = 'bottom',fontsize=15)
plt.show()

   
   
  • 1
  • 2
  • 3

plt.text(x,y,str) #x,y :位置坐标; str :标注内容

                                </div><div><div></div></div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
                            </div>

因篇幅问题不能全部显示,请点此查看更多更全内容

Top