【问题标题】:Values on bar plots and labels条形图和标签上的值
【发布时间】:2021-03-17 04:06:45
【问题描述】:

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

我试图根据它们在 y 轴上的位置将条形的值放在它们的顶部或上方。在 y 轴的正侧,我想将值放在条形上方,在负侧,我想将值放在条形下方。

我还尝试将条形图的标签以与条形图相同的颜色放在图表顶部。

新:

ax.1+plt.bar(r, elements,color=bar_colors,edgecolor='none',width=bardWidth) ax=dataset.plot(width=5.0,kind='bar',y=dataset.columns,figsize=(6,5),zorder=3)

对于 ax.patches 中的 p: ax.annotate("%.2f"%p.get.height(),(p.get_x()+p.get.width()/2.,p.get_height()),ha='center',va ='center',xytext=(0,10),textcoords='offsetpoints')

【问题讨论】:

    标签: python numpy matplotlib


    【解决方案1】:

    您是否正在寻找这样的东西。如果是,请尝试使用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([])
    

    【讨论】:

    • 嘿,感谢您到目前为止的帮助!我试过了,但不幸的是,并非所有数字都显示在图表上,而且它们的位置不正确。你知道这是怎么发生的吗?
    • ax = dataset.plot(width=0.8, kind='bar',y=dataset.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')
    • 1.0051.01 根据您的要求调整它们。
    • 谢谢,我试试! :) 你知道如何解决并非所有值都显示在条形上方或下方的问题吗?
    • 你能更新你的问题并粘贴截图吗?
    猜你喜欢
    • 1970-01-01
    • 2015-05-09
    • 2014-03-10
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多