Python Matplotlib为什么我会得到;ValueError:view limit minimum 0.0小于1,并且是无效的Matplotlib日期值";?

Python Matplotlib为什么我会得到;ValueError:view limit minimum 0.0小于1,并且是无效的Matplotlib日期值";?,python,pandas,matplotlib,finance,Python,Pandas,Matplotlib,Finance,我正在YouTube上浏览Python for Finance教程,但我遇到了一个Matplotlib错误。我的代码从维基百科上抓取了标准普尔500指数的名字,然后使用熊猫数据阅读器从雅虎财经中为每个股票报价器提取股票信息 返回的数据框有一个日期列,每个股票代码名称有一列和两列 使用compile_data()函数,所有这些似乎都可以正常工作,但我得到了ValueError:view limit minimum 0.0小于1,是一个无效的Matplotlib日期值。在尝试使用给定符号(即AAPL

我正在YouTube上浏览Python for Finance教程,但我遇到了一个Matplotlib错误。我的代码从维基百科上抓取了标准普尔500指数的名字,然后使用熊猫数据阅读器从雅虎财经中为每个股票报价器提取股票信息

返回的数据框有一个日期列,每个股票代码名称有一列和两列

使用
compile_data()
函数,所有这些似乎都可以正常工作,但我得到了
ValueError:view limit minimum 0.0小于1,是一个无效的Matplotlib日期值。
在尝试使用给定符号(即AAPL)的Pandas.plot()绘制csv数据帧之后。日期和符号列没有缺少的值

请帮帮我

可以找到教程的完整代码(pythonprogramming.net)

代码如下:

def compile_data():
    with open('sp500tickers.pickle','rb') as f:
        tickers = pickle.load(f)

    main_df = pd.DataFrame()

    for count, ticker in enumerate(tickers):
        df = pd.read_csv('stock_dfs/{}.csv'.format(ticker))
        df.set_index('Date', inplace=True)

        df.rename(columns={'Adj Close': ticker}, inplace=True)
        df.drop(['Open', 'High', 'Low', 'Close', 'Volume'], axis=1, inplace=True)

        if main_df.empty: # boolean for if empty or not
            main_df = df
        else:
            main_df = main_df.join(df, how='outer')

        if count % 100 == 0:
            print(count)

    #print(main_df.head())
    main_df.to_csv('sp500_joined_closes.csv')


compile_data()

def visualize_data():
    df = pd.read_csv('sp500_joined_closes.csv')
    df['AAPL'].plot()
    plt.show()

visualize_data()
以下是完整的错误消息:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
//anaconda3/lib/python3.7/site-packages/pandas/plotting/_tools.py in _handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey)
    306                 for ax in axarr:
--> 307                     layout[ax.rowNum, ax.colNum] = ax.get_visible()
    308 

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

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-87-55ac2958f267> in <module>
      4     plt.show()
      5 
----> 6 visualize_data()

<ipython-input-87-55ac2958f267> in visualize_data()
      1 def visualize_data():
      2     df = pd.read_csv('sp500_joined_closes.csv')
----> 3     df['AAPL'].plot(x='Date')
      4     plt.show()
      5 

//anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in __call__(self, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
   2740                            colormap=colormap, table=table, yerr=yerr,
   2741                            xerr=xerr, label=label, secondary_y=secondary_y,
-> 2742                            **kwds)
   2743     __call__.__doc__ = plot_series.__doc__
   2744 

//anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in plot_series(data, kind, ax, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, label, secondary_y, **kwds)
   1996                  yerr=yerr, xerr=xerr,
   1997                  label=label, secondary_y=secondary_y,
-> 1998                  **kwds)
   1999 
   2000 

//anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in _plot(data, x, y, subplots, ax, kind, **kwds)
   1799         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
   1800 
-> 1801     plot_obj.generate()
   1802     plot_obj.draw()
   1803     return plot_obj.result

//anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in generate(self)
    252         self._add_table()
    253         self._make_legend()
--> 254         self._adorn_subplots()
    255 
    256         for ax in self.axes:

//anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py in _adorn_subplots(self)
    429                                 naxes=nrows * ncols, nrows=nrows,
    430                                 ncols=ncols, sharex=self.sharex,
--> 431                                 sharey=self.sharey)
    432 
    433         for ax in self.axes:

//anaconda3/lib/python3.7/site-packages/pandas/plotting/_tools.py in _handle_shared_axes(axarr, nplots, naxes, nrows, ncols, sharex, sharey)
    325                     if sharex or len(ax.get_shared_x_axes()
    326                                      .get_siblings(ax)) > 1:
--> 327                         _remove_labels_from_axis(ax.xaxis)
    328 
    329         if ncols > 1:

//anaconda3/lib/python3.7/site-packages/pandas/plotting/_tools.py in _remove_labels_from_axis(axis)
    278 
    279 def _remove_labels_from_axis(axis):
--> 280     for t in axis.get_majorticklabels():
    281         t.set_visible(False)
    282 

//anaconda3/lib/python3.7/site-packages/matplotlib/axis.py in get_majorticklabels(self)
   1251     def get_majorticklabels(self):
   1252         'Return a list of Text instances for the major ticklabels.'
-> 1253         ticks = self.get_major_ticks()
   1254         labels1 = [tick.label1 for tick in ticks if tick.label1.get_visible()]
   1255         labels2 = [tick.label2 for tick in ticks if tick.label2.get_visible()]

//anaconda3/lib/python3.7/site-packages/matplotlib/axis.py in get_major_ticks(self, numticks)
   1406         'Get the tick instances; grow as necessary.'
   1407         if numticks is None:
-> 1408             numticks = len(self.get_majorticklocs())
   1409 
   1410         while len(self.majorTicks) < numticks:

//anaconda3/lib/python3.7/site-packages/matplotlib/axis.py in get_majorticklocs(self)
   1323     def get_majorticklocs(self):
   1324         """Get the array of major tick locations in data coordinates."""
-> 1325         return self.major.locator()
   1326 
   1327     def get_minorticklocs(self):

//anaconda3/lib/python3.7/site-packages/matplotlib/dates.py in __call__(self)
   1432     def __call__(self):
   1433         'Return the locations of the ticks'
-> 1434         self.refresh()
   1435         return self._locator()
   1436 

//anaconda3/lib/python3.7/site-packages/matplotlib/dates.py in refresh(self)
   1452     def refresh(self):
   1453         'Refresh internal information based on current limits.'
-> 1454         dmin, dmax = self.viewlim_to_dt()
   1455         self._locator = self.get_locator(dmin, dmax)
   1456 

//anaconda3/lib/python3.7/site-packages/matplotlib/dates.py in viewlim_to_dt(self)
   1203                              'often happens if you pass a non-datetime '
   1204                              'value to an axis that has datetime units'
-> 1205                              .format(vmin))
   1206         return num2date(vmin, self.tz), num2date(vmax, self.tz)
   1207 

ValueError: view limit minimum 0.0 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
//anaconda3/lib/python3.7/site-packages/pandas/plotting//u tools.py in\u handle\u shared\u轴(axarr、nplots、nax、nrows、ncols、sharex、sharey)
306对于axarr中的ax:
-->307布局[ax.rowNum,ax.colNum]=ax.get_visible()
308
索引器:索引5超出大小为3的轴0的界限
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
在里面
4 plt.show()
5.
---->6.数据
在visualize_data()中
1 def visualize_data():
2 df=pd.read\u csv('sp500\u joined\u closes.csv')
---->3 df['AAPL']图(x='Date')
4 plt.show()
5.
//anaconda3/lib/python3.7/site-packages/pandas/plotting//u core.py in\uuuuuu call\uuuuuu(self、kind、ax、figsize、use\u index、title、grid、legend、style、logx、loglog、xticks、yticks、xlim、ylim、rot、fontsize、colormap、table、yerr、xerr、label、secondary\uy、**kwds)
2740 colormap=colormap,table=table,yerr=yerr,
2741 xerr=xerr,label=label,secondary\u y=secondary\u y,
->2742**kwds)
2743调用文件=绘图系列文件__
2744
//anaconda3/lib/python3.7/site-packages/pandas/plotting//u core.py in plot_系列(数据、种类、ax、figsize、使用索引、标题、网格、图例、样式、logx、logy、loglog、xticks、yticks、xlim、ylim、rot、fontsize、colormap、表格、yerr、xer、标签、secondary_y、**kwds)
1996年yerr=yerr,xerr=xerr,
1997年标签=标签,次要y=次要y,
->1998年**科威特第纳尔)
1999
2000
//anaconda3/lib/python3.7/site-packages/pandas/plotting//u core.py in\u plot(数据、x、y、子地块、ax、种类、**kwds)
1799绘图对象=klass(数据,子地块=子地块,ax=ax,种类=种类,**kwds)
1800
->1801绘图对象生成()
1802绘图对象图()
1803返回图_obj.result
//生成(self)中的anaconda3/lib/python3.7/site-packages/pandas/plotting//u core.py
252自我添加表格()
253 self._make_legend()
-->254自我装饰子地块()
255
256对于自轴中的ax:
//anaconda3/lib/python3.7/site-packages/pandas/plotting//u core.py in\u adorn\u子地块(self)
429 naxes=nrows*ncols,nrows=nrows,
430 ncols=ncols,sharex=self.sharex,
-->431 sharey=self.sharey)
432
433对于自轴中的ax:
//anaconda3/lib/python3.7/site-packages/pandas/plotting//u tools.py in\u handle\u shared\u轴(axarr、nplots、nax、nrows、ncols、sharex、sharey)
325如果sharex或len(ax.get_shared_x_axes()
326.获取兄弟姐妹(ax))>1:
-->327 _从_轴(ax.xaxis)移除_标签
328
329如果ncols>1:
//anaconda3/lib/python3.7/site-packages/pandas/plotting//u tools.py in_从_轴(axis)移除_标签
278
279 def_从_轴(轴)上移除_标签:
-->280用于轴中的t。获取_majorticklabels():
281 t.set_可见(假)
282
//get_majorticklabels(self)中的anaconda3/lib/python3.7/site-packages/matplotlib/axis.py
1251 def get_majorticklabels(自我):
1252“返回主要标签的文本实例列表。”
->1253 ticks=self.get_major_ticks()
1254 labels1=[tick.label1表示tick,如果tick.label1.get_visible()]
1255 labels2=[如果tick.label2.get_visible(),则tick.label2代表tick-in-ticks]
//获取主要刻度(self、numticks)中的anaconda3/lib/python3.7/site-packages/matplotlib/axis.py
1406'获取滴答声实例;根据需要成长。”
1407如果numticks为无:
->1408 numticks=len(self.get_majorticklocs())
1409
1410年,当len(自己的专业人士)1325返回self.major.locator()
1326
1327 def get_minorticklocs(自我):
//anaconda3/lib/python3.7/site-packages/matplotlib/dates.py in_u____调用_____(self)
1432定义调用(自):
1433“返回刻度的位置”
->1434 self.refresh()
1435返回自我定位器()
1436
//刷新中的anaconda3/lib/python3.7/site-packages/matplotlib/dates.py(self)
1452 def刷新(自):
1453“根据当前限制刷新内部信息。”
->1454 dmin,dmax=self.viewlim_to_dt()
1455 self.\u定位器=self.get\u定位器(dmin,dmax)
1456
//anaconda3/lib/python3.7/site-packages/matplotlib/dates.py