简体   繁体   English

在matplotlib中的x轴上绘制日期

[英]Drawing dates on x-axis in matplotlib

In the foll. 在foll。 plot, I want to plot dates on the x-axis. 绘制,我想在x轴上绘制日期。 I have a list of dates: 我有一个日期列表:

import arrow as ar
date_plt = '2016-04-01'
_frst_day = '2016-01-01'
_last_day = '2016-12-31'
[ar.get(date_plt) + timedelta(days=x) for x in range((ar.get(_last_day) - ar.get(_frst_day)).days + 1)]

How can I plot dates on the x-axis while still being able to draw the line and the points on the plot (as shown) 如何在x轴上绘制日期,同时仍然能够在图中绘制线条和点(如图所示)

在此处输入图片说明

I think you want (with some code to generate a fake plot): 我认为您想要(使用一些代码来生成假图):

labels=[ar.get(date_plt) + timedelta(days=x) for x in range((ar.get(_last_day)-ar.get(_frst_day)).days + 1)]
x=np.arange(366)
y=x*10
plt.plot([l.datetime for l in labels],y) #convert arrow in datetime

Note that doing this way the datetime objects are plotted and this are autmatically handled by matplotlib, for example in zoom and spacing adjustment (rather than for example plotting on x,y and replacing ticks). 请注意,以这种方式绘制日期时间对象,并由matplotlib自动处理,例如在缩放和间距调整中(而不是例如在x,y上绘制和替换刻度)。 To plot or annotate your data, you can address directly to the datetime x axis, like (example day 150): 要绘制或注释数据,您可以直接定位到datetime x轴,例如(例如第150天):

plt.plot(labels[150].datetime,y[150],'o')
plt.annotate('day 150',(labels[150].datetime,y[150]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM