大熊猫绘制指数超出界限但指数长度相等

时间:2017-07-25 04:29:20

标签: python pandas matplotlib

我试图从我的Pandas DF绘制时间,沿着y轴,x轴上的索引作为一般散点图,但即使我想绘制的值都是相同的长度,我收到以下内容错误:

raise IndexError("indices are out-of-bounds")
IndexError: indices are out-of-bounds

绘制索引(沿x轴)和y轴上的时差的代码如下:

fig = plt.figure()
ax = fig.add_axes([.1,.35,.6,.6])
ax=df.plot(ax=fig.gca(),kind='scatter',x=df.index, y=df.timediff, color='red', edgecolors='black')
plt.show()

df.timediff列的格式如下:

0   1970-01-01 00:19:14
1   1970-01-01 00:19:53
2   1970-01-01 00:23:50
Name: orb_timediff, dtype: datetime64[ns]

和输出:

print(len(df.timediff))
print(len(df.index))
>>
291
291

完整的跟踪堆栈如下:

Traceback (most recent call last):
  File "9_tempscript.py", line 65, in <module>
    ax=df.plot(ax=fig.gca(),kind='scatter',x=df.index,                                            y=df.timediff, color='red', edgecolors='black')
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/tools/plotting.py", line 3774, in __call__
    sort_columns=sort_columns, **kwds)
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/tools/plotting.py", line 2643, in plot_frame
    **kwds)
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/tools/plotting.py", line 2470, in _plot
    plot_obj.generate()
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/tools/plotting.py", line 1043, in generate
    self._make_plot()
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/tools/plotting.py", line 1619, in _make_plot
    scatter = ax.scatter(data[x].values, data[y].values, c=c_va                                                   lues,
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/core/frame.py", line 2053, in __getitem__
    return self._getitem_array(key)
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/core/frame.py", line 2098, in _getitem_array
    return self.take(indexer, axis=1, convert=True)
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/core/generic.py", line 1669, in take
    convert=True, verify=True)
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/core/internals.py", line 3955, in take
    indexer = maybe_convert_indices(indexer, n)
  File "/opt/antelope/python2.7.8/lib/python2.7/site-packages/p                                                   andas/core/indexing.py", line 1873, in maybe_convert_indices
    raise IndexError("indices are out-of-bounds")
IndexError: indices are out-of-bounds

1 个答案:

答案 0 :(得分:2)

有问题,你需要两列数字。

所以可能的解决方案是转换to_timedelta,然后转换为dt.total_seconds seconds

df['orb_timediff'] = pd.to_timedelta(df['orb_timediff']).dt.total_seconds().astype(int)
df = df.reset_index()
print (df)
   index  orb_timediff
0      0          1154
1      1          1193
2      2          1430

fig = plt.figure()
ax = fig.add_axes([.1,.35,.6,.6])
ax=df.plot(ax=fig.gca(), 
           kind='scatter',
           x='index', 
           y='orb_timediff', 
           color='red', 
           edgecolors='black')
plt.show()

graph