Chris L.
Chris L.

Reputation: 21

Matplotlib does not show dates on the chart using datareader

I am fairly new to using Python and trying to display a simple stock chart. I have seen similar questions using dateFrame but not with datareader.

Here is a sample of the code. The issue is that the dates don't appear at the bottom. Not sure if I need to or to state that its the index.

import pandas_datareader.data as web
import datetime
import matplotlib.pyplot as plt
%matplotlib inline

start = datetime.datetime(2017, 1, 1)
end = datetime.datetime(2018, 6, 8)

stock = web.DataReader('GOOG', 'iex', start, end)
stock_name = 'Google'

fig = plt.figure()
ax = fig.add_subplot(1,1,1) 

stock['close'].plot(ax=ax,grid = True, color='blue',fontsize=14,legend=False) 
ax.set_title(stock_name, fontsize=18)
ax.set_xlabel('Date',fontsize=14)

Upvotes: 1

Views: 1083

Answers (1)

muzzyq
muzzyq

Reputation: 904

At the moment, your index is of type "object". You just need to convert it to "datetime". Try the below after you read in the DataFrame.

stock.index = pd.to_datetime(stock.index)

Upvotes: 1

Related Questions