使用Python的matplotlib在x轴上绘制日期

时间:2022-09-10 22:02:11

I am trying to plot information against dates. I have a list of dates in the format "01/02/1991".

我正在尝试根据日期绘制信息。我有一个日期列表,格式为“01/02/1991”。

I converted them by doing the following:

我通过执行以下操作转换它们:

x = parser.parse(date).strftime('%Y%m%d'))

which gives 19910102

这给了19910102

Then I tried to use num2date

然后我尝试使用num2date

import matplotlib.dates as datesnew_x = dates.num2date(x)

Plotting:

plt.plot_date(new_x, other_data, fmt="bo", tz=None, xdate=True)

But I get an error. It says "ValueError: year is out of range". Any solutions?

但是我收到了一个错误。它说“ValueError:年份超出范围”。有解决方案?

2 个解决方案

#1


14  

As @KyssTao has been saying, help(dates.num2date) says that the x has to be a float giving the number of days since 0001-01-01 plus one. Hence, 19910102 is not 2/Jan/1991, because if you counted 19910101 days from 0001-01-01 you'd get something in the year 54513 or similar (divide by 365.25, number of days in a year).

正如@KyssTao所说,help(dates.num2date)表示x必须是一个浮点数,给出自0001-01-01以来的天数加一。因此,19910102不是1991年1月2日,因为如果您从0001-01-01计算19910101天,您将获得54513年或类似的东西(除以365.25,一年中的天数)。

Use datestr2num instead (see help(dates.datestr2num)):

请改用datestr2num(请参阅help(dates.datestr2num)):

new_x = dates.datestr2num(date) # where date is '01/02/1991'

#2


90  

You can do this more simply using plot() instead of plot_date().

您可以使用plot()而不是plot_date()更简单地完成此操作。

First, convert your strings to instances of Python datetime.date:

首先,将您的字符串转换为Python datetime.date的实例:

import datetime as dtdates = ['01/02/1991','01/03/1991','01/04/1991']x = [dt.datetime.strptime(d,'%m/%d/%Y').date() for d in dates]y = range(len(x)) # many thanks to Kyss Tao for setting me straight here

Then plot:

import matplotlib.pyplot as pltimport matplotlib.dates as mdatesplt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))plt.gca().xaxis.set_major_locator(mdates.DayLocator())plt.plot(x,y)plt.gcf().autofmt_xdate()

Result:

使用Python的matplotlib在x轴上绘制日期

#1


14  

As @KyssTao has been saying, help(dates.num2date) says that the x has to be a float giving the number of days since 0001-01-01 plus one. Hence, 19910102 is not 2/Jan/1991, because if you counted 19910101 days from 0001-01-01 you'd get something in the year 54513 or similar (divide by 365.25, number of days in a year).

正如@KyssTao所说,help(dates.num2date)表示x必须是一个浮点数,给出自0001-01-01以来的天数加一。因此,19910102不是1991年1月2日,因为如果您从0001-01-01计算19910101天,您将获得54513年或类似的东西(除以365.25,一年中的天数)。

Use datestr2num instead (see help(dates.datestr2num)):

请改用datestr2num(请参阅help(dates.datestr2num)):

new_x = dates.datestr2num(date) # where date is '01/02/1991'

#2


90  

You can do this more simply using plot() instead of plot_date().

您可以使用plot()而不是plot_date()更简单地完成此操作。

First, convert your strings to instances of Python datetime.date:

首先,将您的字符串转换为Python datetime.date的实例:

import datetime as dtdates = ['01/02/1991','01/03/1991','01/04/1991']x = [dt.datetime.strptime(d,'%m/%d/%Y').date() for d in dates]y = range(len(x)) # many thanks to Kyss Tao for setting me straight here

Then plot:

import matplotlib.pyplot as pltimport matplotlib.dates as mdatesplt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))plt.gca().xaxis.set_major_locator(mdates.DayLocator())plt.plot(x,y)plt.gcf().autofmt_xdate()

Result:

使用Python的matplotlib在x轴上绘制日期