import matplotlib.pyplot as plt
import numpy as np
# 准备数据
x = np.array(['A', 'B', 'C', 'D', 'E'])
y = np.array([50, 30,40, 50, 80])
# 绘制柱状图
fig, ax = plt.subplots()
rects = ax.bar(x, y)
# 添加数值标签
for rect in rects:
height = rect.get_height()
ax.annotate(f'{height}',
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
# 显示图形
plt.show()
我搜遍全网也没有发现那种简单的”一键开启”的方法😂,好像在python中用matplotlib绘制柱状图需要加数值便签的时候, 都要像图上的代码一样,先计算数值的位置。 有没有大佬知道或者用过别的好点的方法?