Reputation: 271
I am getting the following error when plotting. I am using Visual Studio Code with Windows 10.
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
#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()
Full error stack:
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
Upvotes: 0
Views: 686
Reputation: 5247
Not sure if this is still relevant for you, but you were plotting strings here: plt.plot('ShortEMA', ... )
instead of actually plotting the object: plt.plot(ShortEMA, ... )
(Note the quotes).
Upvotes: 1