0

When I run df.index, it outputs the format as YYYY-MM-DD, which is how I would like it viewed on the figure. Instead, at each major tick the values are, for example, 1.48 1e18, 1.50 1e18, etc. Is there a simple way to convert to the aforementioned format?

from datetime import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web

start = dt(2016, 6, 1)
end = dt.now()

df = web.get_data_yahoo('AAPL', start, end, interval = 'd')

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(df.index, df['Adj Close'], label='ADJ CLOSE')
ax.set_xlabel('Date')
ax.set_ylabel('Price')
ax.legend()
plt.show()

Using Python 3.6.2 and pandas 0.21.0

NaN
  • 643
  • 1
  • 8
  • 21
  • Or of https://stackoverflow.com/questions/32972371/how-to-show-date-and-time-on-x-axis-in-matplotlib – Tom de Geus Dec 31 '17 at 07:20
  • @Tom de Geus I've tried using `fig.autofmt_xdate()` but it does not change the format still. – NaN Dec 31 '17 at 22:52
  • Please provide a [mcve] of the issue and clearly state which matplotlib and pandas versions you are using. – ImportanceOfBeingErnest Jan 09 '18 at 08:34
  • @ImportanceOfBeingErnest Python 3.6.2 and pandas 0.21.0 – NaN Jan 13 '18 at 02:06
  • I would guess you run into [this problem](https://stackoverflow.com/questions/47404653/pandas-0-21-0-timestamp-compatibility-issue-with-matplotlib). In that case adding `import pandas.plotting._converter as pandacnv; pandacnv.register()` would solve the issue. – ImportanceOfBeingErnest Jan 13 '18 at 02:12
  • @ImportanceOfBeingErnest Thank you this worked for me, still not totally sure why, but do you want to add it as an answer below that I can accept for you? – NaN Jan 14 '18 at 19:31
  • If that solves the problem, one may simply mark as duplicate. The reason is explained in the duplicate and the linked issues within. – ImportanceOfBeingErnest Jan 15 '18 at 09:56

0 Answers0