【问题标题】:Python matplotlib: add count number on top of the bar [duplicate]Python matplotlib:在条形图顶部添加计数[重复]
【发布时间】:2025-12-25 12:15:11
【问题描述】:

我有以下 matplotlib 代码:

ax = my_df.plot(x='arrival_month', y='my_count' ,kind='bar', figsize =  (20,10), fontsize = 16, color = 'b', alpha = 0.3)  
ax.set_xticklabels([t.get_text().split(" ")[0] for t in ax.get_xticklabels()])
ax.set_ylim([-50,300])
plt.show()

生成图形:

我想知道是否可以在条形顶部添加计数:

非常感谢!

【问题讨论】:

    标签: python-3.x matplotlib bar-chart


    【解决方案1】:

    类似循环的东西

    ax.annotate('(%s, %s)' % xy, xy=xy, textcoords='data')

    应该可以,其中xy 是表示每个坐标的 x 和 y 值的元组。

    这已在answer to another question 中提出。

    如果您提供 MWE,我们也可以测试我们的建议。尤其是告诉我们my_df 到底长什么样会有所帮助。

    【讨论】: