在绘图中设置xlim时出现值错误

2024-04-29 00:54:34 发布

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

我有以下代码:

stock = '^GSPC'
data = web.DataReader(stock, 'yahoo', start, end)
data['month'] = data.index.month
data['day'] = data.index.day
data.set_index('month',append=True,inplace=True)
data.set_index('day',append=True,inplace=True)
df = data.groupby(['month', 'day']).mean()
df.plot(y = 'Close');

情节如下所示:

enter image description here

我一直在搞这个阴谋

enter image description here

如何设置xlim

在正式文件中: https://pandas.pydata.org/pandas-docs/version/0.14/generated/pandas.DataFrame.plot.html此格式如下所示:

DataFrame.plot(xlim = '')

因此:

df.plot(y = 'Close', xlim =('df.index[0]', 'df.index[-1]'));

此图仅持续到(1,3),但df.index[-1]的值为:

Out[74]: (12, 31)

如何确定xlim的最终值

编辑:

我想做的是让我的情节看起来像这样: enter image description here

正如您所看到的,它从y轴的一个脊椎开始,并在绘图的末尾结束。在开始之前和结束之后都没有“空白”。不管怎样,我可以让情节在没有“间隙”的情况下开始和结束吗