python plt.bar上增加数据标签
时间: 2023-11-24 15:01:53 浏览: 69
在Python中使用matplotlib模块绘制数据图的示例
Sure, I'd be happy to help you with that! Here is some sample code to add data labels to a bar chart in Python using matplotlib:
```
import matplotlib.pyplot as plt
x = ['A', 'B', 'C']
y = [1, 2, 3]
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), textcoords="offset points", ha='center', va='bottom')
plt.show()
```
This will add the value of each data point above the corresponding bar. I hope this helps!
阅读全文