【问题标题】:view limit minimum -36816.4 is less than 1 and is an invalid Matplotlib date value查看限制最小值 -36816.4 小于 1 并且是无效的 Matplotlib 日期值
【发布时间】:2021-02-27 09:02:29
【问题描述】:

绘图时出现以下错误。我在 Windows 10 中使用 Visual Studio Code。

文件“C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\dates.py”,第 1098 行,在 viewlim_to_dt
.format(vmin)) ValueError: view limit minimum -36816.4 小于 1 并且是无效的 Matplotlib 日期值。如果您将非日期时间值传递给具有日期时间单位的轴,这通常会发生

#Store the data in a variable
df = pd.read_csv('data/Tesla_Stock.csv')

#Set the index
df = df.set_index(pd.DatetimeIndex(df['Date'].values))
df['Date'] = pd.to_datetime(df['Date'])

### TRIED THIS. DOES NOT WORK
# df = df.set_index('Date', inplace=True)


#Show the data
print(df.info())
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 1258 entries, 2012-01-03 to 2016-12-30
Data columns (total 7 columns):
Date         1258 non-null datetime64[ns]
High         1258 non-null float64
Low          1258 non-null float64
Open         1258 non-null float64
Close        1258 non-null float64
Volume       1258 non-null int64
Adj Close    1258 non-null float64
dtypes: datetime64[ns](1), float64(5), int64(1)


#Calculate the three moving averages
ShortEMA = df.Close.ewm(span=5, adjust = False).mean()
MiddleEMA = df.Close.ewm(span=21, adjust = False).mean()
LongEMA = df.Close.ewm(span=63, adjust = False).mean()

#Visualize the closing price and the exponential moving avearges
plt.figure(figsize=(12.2, 4.5))
plt.title('Close Price', fontsize = 18)
plt.plot(df['Close'], label = 'Close Price', color = 'blue')

# ERRORS WHEN THE BELOW THREE LINES ARE INCLUDED
plt.plot('ShortEMA', label = 'Short/Fast EMA', color = 'red')
plt.plot('MiddleEMA', label = 'Middle/Medium EMA', color = 'orange')
plt.plot('LongEMA', label = 'Long/Slow EMA', color = 'green')

plt.xlabel('Date', fontsize = 18)
plt.ylabel('Close Price', fontsize = 18)
plt.show()

完整的错误堆栈:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 745, in callit
    func(*args)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\_backend_tk.py", line 270, in idle_draw
    self.draw()
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
    super(FigureCanvasTkAgg, self).draw()
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 393, in draw   
    self.figure.draw(self.renderer)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\figure.py", line 1736, in draw
    renderer, self, artists, self.suppressComposite)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\image.py", line 137, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes\_base.py", line 2630, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\image.py", line 137, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\artist.py", line 38, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axis.py", line 1227, in draw
    ticks_to_draw = self._update_ticks()
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axis.py", line 1103, in _update_ticks
    major_locs = self.get_majorticklocs()
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axis.py", line 1348, in get_majorticklocs     
    return self.major.locator()
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\dates.py", line 1338, in __call__
    self.refresh()
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\dates.py", line 1364, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "C:\Users\bitrun\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\dates.py", line 1098, in viewlim_to_dt        
    .format(vmin))
ValueError: view limit minimum -36816.4 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

【问题讨论】:

    标签: python pandas dataframe matplotlib


    【解决方案1】:

    不确定这是否仍然与您相关,但您在此处绘制字符串:plt.plot('ShortEMA', ... ) 而不是实际绘制对象:plt.plot(ShortEMA, ... )(注意引号)。

    【讨论】:

      猜你喜欢
      • 2019-08-27
      • 2020-11-28
      • 1970-01-01
      • 2019-07-24
      • 2019-03-05
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 2020-08-10
      相关资源
      最近更新 更多