【发布时间】:2021-12-25 13:19:21
【问题描述】:
我很难将数据标签添加到我正在创建的 matplotlib 图形中。在条形图上我没有问题。 为了更容易进行故障排除,我尽可能地简化了它,但仍然存在同样的问题。
我一直在苦苦寻找,但找不到答案......
import matplotlib.pyplot as plt
dates = [10,11,12]
temp = [10,14,12]
temp_labels = plt.plot(dates,temp)
for x in temp_labels:
label = temp[x]
plt.annotate(label,
(x,temp[x]),
textcoords = "offset points"),
xytext = (0,10),
ha = "center")
plt.show()
我遇到了一个错误:
Traceback (most recent call last):
File "rain_notif.py", line 16, in <module>
label = temp[x]
TypeError: list indices must be intergers or slices, not Line2D
这就是我所拥有的,我只想在每个点的顶部加上标签。 Figure without label
【问题讨论】:
标签: python matplotlib graph label