条形图和标签上的值

2024-10-02 18:16:00 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here我是Python编程的初学者,我需要你的帮助

我试着根据横条在y轴上的位置,将横条的值放在横条的顶部或上方。在y轴的正侧,我想把值放在条的上方,在负侧,我想把值放在条的下方

我也在尝试把这些条的标签放在图的顶部,颜色和这些条一样

新的:

ax.1+plt.bar(r,元素,颜色=条形图颜色,边色=无,宽度=宽度) ax=dataset.plot(宽度=5.0,种类=bar',y=dataset.columns,figsize=(6,5),zorder=3)

对于ax.patches中的p: ax.注释(“%.2f”%p.get.height(),(p.get\u x()+p.get.width()/2.,p.get\u height()),ha='center',va='center',xytext=(0,10),textcoords='offsetpoints')


Tags: imageget宽度here颜色编程bardescription
1条回答
网友
1楼 · 发布于 2024-10-02 18:16:00

你在找这样的东西吗。如果是,则尝试使用ax.patches在条形图上输入值

df:

    a   b   c   d
0   66  92  98  17
1  -83  57  86  97
2   96  47 -73  32

ax = df.plot(width=0.8, kind='bar',y=df.columns,figsize=(10,5))
for p in ax.patches:
    ax.annotate(str(round(p.get_height(),2)), (p.get_x() * 1.005, p.get_height() * 1.01),color='black')
  
ax.axes.get_yaxis().set_ticks([])

enter image description here

相关问题 更多 >