升级 matplotlib 后,我失去了针对日期时间对象进行绘图的能力。
错误如下:
File "/opt/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2813, in plot
is not None else {}), **kwargs)
File "/opt/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/__init__.py", line 1810, in inner
return func(ax, *args, **kwargs)
File "/opt/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1611, in plot
for line in self._get_lines(*args, **kwargs):
File "/opt/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 393, in _grab_next_args
yield from self._plot_args(this, kwargs)
File "/opt/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 370, in _plot_args
x, y = self._xy_from_xy(x, y)
File "/opt/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 204, in _xy_from_xy
bx = self.axes.xaxis.update_units(x)
File "/opt/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axis.py", line 1475, in update_units
self.set_units(default)
File "/opt/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axis.py", line 1548, in set_units …Run Code Online (Sandbox Code Playgroud) 下图
import matplotlib
f= plt.figure(figsize=(12,4))
ax = f.add_subplot(111)
df.set_index('timestamp')['values'].plot(ax=ax)
ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval=1))
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%I'))
plt.show()
Run Code Online (Sandbox Code Playgroud)
即使我有以下情况,也会在错误的区域(GMT)中显示小时:
> df['timestamp'][0]
Timestamp('2014-09-02 18:37:00-0400', tz='US/Eastern')
Run Code Online (Sandbox Code Playgroud)
实际上,如果我注释掉这一行:
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%I'))
Run Code Online (Sandbox Code Playgroud)
在正确的时区中显示小时。
为什么?