gpt4 book ai didi

python - 以日期为轴在 matplotlib 中定位文本

转载 作者:行者123 更新时间:2023-12-05 06:29:54 25 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 中的 .text() 方法定位文本。我的 x 轴格式为 datetime64。它像宣传的那样工作,但我仍然收到如下所示的错误。

我的代码:

fix, ax = plt.subplots()
ax.plot(x,y)
ax.text('2014-11-01', 82, 'Text goes here', fontsize=26, weight='bold', alpha=.8)
plt.text('2014-11-01', 77, s= 'Subtitle text goes here', fontsize=18)

按预期返回一个图: plot with text

此外,还引发了一个错误:

TypeError: must be real number, not str

我对此很困惑。我也尝试通过 matplotlib 的 dates.date2num(t) 方法传入一个数字,但没有成功。

最佳答案

您需要将要插入的实际文本之前的坐标传递给.text()。例如:

>>> text(0.5, 0.5,'matplotlib', horizontalalignment='center',
... verticalalignment='center',
... transform=ax.transAxes)

您遇到的错误是由于在方法需要数字的地方传递了一个字符串 ('2014-11-01') 引起的。

参见 docs .

关于python - 以日期为轴在 matplotlib 中定位文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53001002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com