使用Gridspec绘制:索引超出界限

2024-05-23 17:49:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我以前用了很多gridspec,但是今天我遇到了一个我从未遇到过的错误。下面是一个代码示例,取自pandas plotting example page:

import pandas as pd
from pandas import *
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()

df3 = pd.DataFrame(np.random.randn(1000, 2), columns=['B', 'C']).cumsum()
df3['A'] = pd.Series(list(range(len(df))))

gs = gridspec.GridSpec(5,1)
fig = plt.figure(figsize=(6, 6), facecolor='white')
ax = fig.add_subplot(gs[:4, :])
ax2 = fig.add_subplot(gs[4, :])

df3.plot(x='A', y='B', ax=ax2)

当它正在打印时,我有一个错误导致程序停止:

---> 21 df3.plot(x='A', y='B', ax=ax2)

IndexError: index 4 is out of bounds for axis 0 with size 3

{1美元^

注意:错误消息中的index 4直接链接到gridspec限制。如果我删除gridspec,绘图效果会很好。在

熊猫版本:0.16.1

Matplotlib版本:1.4.3


Tags: importgspandasdfindexas错误np