【问题标题】:Annotate a plot using matplotlib - showing values in the plot使用 matplotlib 注释绘图 - 在绘图中显示值
【发布时间】:2017-08-26 01:23:55
【问题描述】:

我有两个列表:一个包含字符串 (liste_string) 和一个包含数字的列表 (liste_summe)。 带有字符串的列表包含日期值作为字符串。 我想在图中显示 liste_summe 的值,但它不起作用。

 plt.subplots_adjust(left=None, bottom=0.4, right=None, top=None, wspace=None, hspace=None)#Grösse des Fenster einrichten
plt.annotate(liste_summe, xy = (liste_summe, liste_datum))
plt.xticks(rotation = 90)#Schrägstellen der Beschriftung der x-Achse um x Grad
plt.ylim([y_min, y_max])
plt.plot(liste_string, liste_summe)
plt.show()

当我执行这段代码时,我得到了这个错误:

TypeError: float() argument must be a string or a number, not 'list'

有人有想法吗?

【问题讨论】:

  • 错误出现在哪一行?请记住,在报告错误时,请包含完整的回溯。
  • 如果您需要这方面的帮助,您需要显示您的数据。

标签: python matplotlib tkinter


【解决方案1】:

plt.annotate 不能用于列表。正如在♠the documentation 中看到的那样,它想要一个字符串作为它的第一个参数,一个坐标对作为它的第二个参数。由于 x 值是日期,坐标元组中的第一个条目必须是日期(而不是相反)。为了注释所有点,可以使用循环;每点调用一次plt.annotate 进行注释。

import matplotlib.pyplot as plt

liste_summe = [1,2,3,1,4]
liste_datum = ["2017-02-05", "2017-02-06", "2017-02-09", "2017-02-12", "2017-02-24"]
liste_string = liste_datum

plt.figure()
plt.subplots_adjust(bottom=0.4)
for s, d in zip(liste_summe, liste_datum):
    plt.annotate(d, xy = (d,s ))
plt.xticks(rotation = 90)

plt.plot_date(liste_string, liste_summe)
plt.show()

【讨论】:

  • 如果我执行你的代码我得到这个错误:文件“C:\Python34\lib\site-packages\matplotlib\dates.py”,第 220 行,在 _to_ordinalf base = float(dt.toordinal ())
  • 如果您的 matplotlib 版本为 1.5 或以下,它可能无法正常工作。
  • 我有2.0版
  • 好吧,不知道怎么了。问题是我找不到替代品并对其进行测试,因为对我来说这个解决方案已经奏效了。 (使用 python 2.7 和 matplotlib 2.0)
  • 现在,由于您没有在问题中提供数据,因此获得另一个答案的机会很低。此外,由于TypeError: float() argument must be a string or a number, not 'list' 的初始问题已通过此答案解决,我可能会提出以下建议:您可以接受此答案并在新问题中使用此答案中的代码,明确说明据报道它可以工作(链接这个问题),但是您的系统出现错误(包括有关系统的完整信息以及 full 错误回溯)。在工作日发布这个新问题可能会更成功。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
  • 2016-06-07
  • 2014-03-09
  • 2012-06-05
  • 1970-01-01
  • 2021-03-14
  • 2021-05-13
相关资源
最近更新 更多