import numpy as np
import matplotlib.pyplot as plt
# 生成测试数据
x = np.linspace(0, 10, 11)
y = 11-x
# 绘制柱状图
plt.bar(x, y)
# 循环,为每个柱形添加文本标注
# 居中对齐
for xx, yy in zip(x,y):
plt.text(xx, yy+0.1, str(yy), ha='center')
# 显示图形
plt.show()
运行效果:
import numpy as np
import matplotlib.pyplot as plt
# 生成测试数据
x = np.linspace(0, 10, 11)
y = 11-x
# 绘制柱状图
plt.bar(x, y)
# 循环,为每个柱形添加文本标注
# 居中对齐
for xx, yy in zip(x,y):
plt.text(xx, yy+0.1, str(yy), ha='center')
# 显示图形
plt.show()
运行效果: