Python 如何使用x_loc作为datetime对象在matplolib图上放置ax.text
当我尝试Python 如何使用x_loc作为datetime对象在matplolib图上放置ax.text,python,matplotlib,Python,Matplotlib,当我尝试 ax2.text(x=datetime.datetime(2016,1,1),y=0,s='Voila') TypeError: a float is required 同时,我可以使用datetime对象作为xlocation更改同一绘图中区域的颜色。 ax2.axvspandatetime.datetime2016,1,1,datetime.datetime2018,1,1,facecolor='gray'。 如何使用datetime作为xlocation在绘图上放置文本?您
ax2.text(x=datetime.datetime(2016,1,1),y=0,s='Voila')
TypeError: a float is required
同时,我可以使用datetime对象作为xlocation更改同一绘图中区域的颜色。
ax2.axvspandatetime.datetime2016,1,1,datetime.datetime2018,1,1,facecolor='gray'。
如何使用datetime作为xlocation在绘图上放置文本?您需要将datetime对象转换为数字。为此,您可以使用内置机制
import matplotlib.dates as mdates
ax.text(mdates.date2num(datetime.datetime(2016,1,1)), 0, "Voilà")