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

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

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 dates
new_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: year is out of range”。有解决方案吗?

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一直在说,帮助(data .num2date)说,x必须是一个浮点数,提供自0001-01-01 + 1的天数。因此,19910102不是2/Jan/1991,因为如果你算上19910101,从0001-01-01开始,你会得到54513或类似的年份(除以365.25,一年中的天数)。

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

相反,使用datestr2num(请参见帮助(data .datestr2num)):

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

#2


82  

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数据时间的实例。

import datetime as dt

dates = ['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 plt
import matplotlib.dates as mdates

plt.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一直在说,帮助(data .num2date)说,x必须是一个浮点数,提供自0001-01-01 + 1的天数。因此,19910102不是2/Jan/1991,因为如果你算上19910101,从0001-01-01开始,你会得到54513或类似的年份(除以365.25,一年中的天数)。

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

相反,使用datestr2num(请参见帮助(data .datestr2num)):

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

#2


82  

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数据时间的实例。

import datetime as dt

dates = ['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 plt
import matplotlib.dates as mdates

plt.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轴上绘制日期。