【发布时间】:2018-04-13 09:50:33
【问题描述】:
我有几个这样的数据帧:
>>> roll_av.shape
Out[14]: (274, 2)
>>> roll_av.tail()
Out[12]:
week month
Date
2017-10-12 0.001 -0.016
2017-10-13 0.231 0.020
2017-10-16 0.180 0.018
2017-10-17 0.235 0.021
2017-10-18 0.187 0.020
>>> roll_av.index
Out[1]: Index([u'2016-08-11', u'2016-08-12', u'2016-08-13', u'2016-08-14',
u'2017-06-08', ....... u'2017-10-16', u'2017-10-17', u'2017-10-18'],
dtype='object', name=u'Date')
如果我这样做,我会得到一个以 x 轴为日期的图:
roll_av.plot()
但是,如果我执行以下操作,日期就会消失。
fig = plt.figure(1)
for i in np.arange(4):
ax = fig.add_subplot(221 + i)
roll_avg = get_roll_avg(params)
ax.plot(roll_av)
改为这样做时,我得到一个错误:
>>> ax.plot(roll_av.index,roll_av)
Traceback (most recent call last):
File "C:\...\matplotlib\axes\_axes.py", line 1374, in plot
self.add_line(line)
File "C:\...\matplotlib\axes\_base.py", line 1504, in add_line
self._update_line_limits(line)
File "C:\...\matplotlib\axes\_base.py", line 1515, in _update_line_limits
path = line.get_path()
File "C:...\matplotlib\lines.py", line 874, in get_path
self.recache()
File "C:\...\matplotlib\lines.py", line 575, in recache
x = np.asarray(xconv, np.float_)
File "C:\...\numpy\core\numeric.py", line 460, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: invalid literal for float(): 2017-10-18
有什么简单的方法可以解决这个问题吗?
【问题讨论】:
-
get_roll_avg在做什么?似乎正在将您的索引转换为字符串。 -
已编辑以显示索引。我应该将索引转换为日期时间吗?
-
@dayum,是的,将
index转换为 DateTime。它会起作用的。
标签: python datetime matplotlib plot subplot