How to display the value of the bar on each bar with pyplot.barh()?

后端 未结 9 758
不知归路
不知归路 2020-11-22 08:10

I generated a bar plot, how can I display the value of the bar on each bar?

Current plot:

\"enter

9条回答
  •  孤城傲影
    2020-11-22 08:38

    I was trying to do this with stacked plot bars. The code that worked for me was.

    # Code to plot. Notice the variable ax.
    ax = df.groupby('target').count().T.plot.bar(stacked=True, figsize=(10, 6))
    ax.legend(bbox_to_anchor=(1.1, 1.05))
    
    # Loop to add on each bar a tag in position
    for rect in ax.patches:
        height = rect.get_height()
        ypos = rect.get_y() + height/2
        ax.text(rect.get_x() + rect.get_width()/2., ypos,
                '%d' % int(height), ha='center', va='bottom')
    

提交回复
热议问题