Skip to main content
 首页 » 编程设计

python之matplotlib: xaxis dates ValueError: format %y requires year >= 1900 on Windows

2024年11月24日33yxwkf

我从 pandas-matplotlib 收到一个我不明白的神秘错误。

如果我有以下 df:

     Agency   Gage_no       date  something flag     elev 
0      USGS  12392500 1996-01-01      55.38    A  2055.38 
1000   USGS  12392500 1998-09-27      60.10    A  2060.10 
2000   USGS  12392500 2001-06-23      61.90    A  2061.90 
3000   USGS  12392500 2004-03-19      51.63    A  2051.63 
4000   USGS  12392500 2006-12-14      52.51    A  2052.51 
5000   USGS  12392500 2009-09-09      62.23    A  2062.23 
6000   USGS  12392500 2012-06-05      60.06    A  2060.06 
7000   USGS  12392500 2015-03-02      52.06    A  2052.06 
8000   USGS  12392500 2017-11-26      51.60    P  2051.60 

我想绘制并使用 matplotlib.dates 来格式化 x 轴:

df['date'] = pd.to_datetime(df['date']) 
 
fig, ax = plt.subplots() 
 
df.plot(x='date',y='elev', ax=ax) 
 
years = dates.YearLocator(10,month=1,day=1) 
years1=dates.YearLocator(5,month=1,day=1) 
dfmt = dates.DateFormatter('%Y') 
dfmt1 = dates.DateFormatter('%y') 
 
[i.xaxis.set_major_locator(years) for i in fig.axes] 
[i.xaxis.set_minor_locator(years1) for i in fig.axes] 
[i.xaxis.set_major_formatter(dfmt) for i in fig.axes] 
[i.xaxis.set_minor_formatter(dfmt1) for i in fig.axes] 

我收到一个回溯信息,表明我正在尝试对 1900 年之前的日期使用日期 format='%y'。

Out[40]: [None]Traceback (most recent call last): 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\backends\backend_qt5.py", line 501, in _draw_idle 
    self.draw() 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\backends\backend_agg.py", line 388, in draw 
    self.figure.draw(self.renderer) 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper 
    return draw(artist, renderer, *args, **kwargs) 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\figure.py", line 1709, in draw 
    renderer, self, artists, self.suppressComposite) 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\image.py", line 135, in _draw_list_compositing_images 
    a.draw(renderer) 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper 
    return draw(artist, renderer, *args, **kwargs) 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\axes\_base.py", line 2645, in draw 
    mimage._draw_list_compositing_images(renderer, self, artists) 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\image.py", line 135, in _draw_list_compositing_images 
    a.draw(renderer) 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper 
    return draw(artist, renderer, *args, **kwargs) 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\axis.py", line 1204, in draw 
    ticks_to_draw = self._update_ticks() 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\axis.py", line 1089, in _update_ticks 
    minor_labels = self.minor.formatter.format_ticks(minor_locs) 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\ticker.py", line 259, in format_ticks 
    return [self(value, i) for i, value in enumerate(values)] 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\ticker.py", line 259, in <listcomp> 
    return [self(value, i) for i, value in enumerate(values)] 
  File "C:\Anaconda3\envs\gdal_env\lib\site-packages\matplotlib\dates.py", line 608, in __call__ 
    return num2date(x, self.tz).strftime(self.fmt) 
ValueError: format %y requires year >= 1900 on Windows 

为什么会这样,显然我的约会只能追溯到 1996 年?

如果我使用 ax.plot_date 而不是 df.plot,则绘图会正确呈现。

请您参考如下方法:

考虑到给定的 x 和 y,这就是我通常处理日期的方式

import matplotlib.dates as mdates 
 
fig = plt.figure() 
ax = fig.add_subplot() 
 
ax.plot_date(x =, y = ) 
plt.gca().xaxis.set_major_locator(mdates.AutoDateLocator()) 
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S')) # format as you wish 
ax.set_xticks(x) 
 
plt.show()