- 柱状图加数值也是一项常规操作,这里采用的函数是text,主要获取加入text的位置与数值即可,因此,详细代码如下:
- 这里写了auto_label与auto_text,两个都可以用,本人更喜欢用auto_text
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
def auto_label(rects):
for rect in rects:
height = rect.get_height()
plt.annotate('{}'.format(height), # put the detail data
xy=(rect.get_x() + rect.get_width() / 2, height), # get the center location.
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
def auto_text(rects):
for rect in rects:
plt.text(x=rect.get_x()+0.1, y=rect.get_height(), s=rect.get_height(), ha='left', va='bottom')
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 34, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
index = np.arange(len(labels))
width = 0.2
fig, ax = plt.subplots()
rect1 = ax.bar(index - width / 2, men_means, color ='lightcoral', width=width, label ='Men')
rect2 = ax.bar(index + width / 2, women_means, color ='springgreen', width=width, label ='Women')
ax.set_title('Scores by gender')
ax.set_xticks(ticks=index)
ax.set_xticklabels(labels)
ax.set_ylabel('Scores')
ax.set_ylim(0, 50)
# auto_label(rect1)
# auto_label(rect2)
auto_text(rect1)
auto_text(rect2)
ax.legend(loc='upper right', frameon=False)
fig.tight_layout()
plt.savefig('2.tif', dpi=300)
plt.show()
转自:Python Matplotlib 柱状图加数值_Sound_of_ Silence的博客-优快云博客_python柱状图加数字
该博客介绍了如何使用Python的Matplotlib库在柱状图上添加数值,通过`auto_label`和`auto_text`两个函数实现。示例代码展示了创建男女成绩对比的柱状图,并在每个柱子上标注具体数值的方法,有助于增强图表的可读性。
1769

被折叠的 条评论
为什么被折叠?



