I am trying to improve the x-axis format on a bar chart of a time series with a lot of data. By default Matplotlib adds a date label for each data, looking like this:
import pandas as pd
import pandas_datareader as pdr
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
AAPL = pdr.DataReader("AAPL", 'yahoo', "2020-01-01")["Adj Close"]
fig, ax = plt.subplots()
AAPL.plot(kind="bar", figsize=(15,6), grid=True, ax=ax)
plt.show()
I also tried Matplotlib.dates (mdates) and then add format (year-month) and got a better display, but the problem arose that the date values are changed to the year 1970.
fig, ax = plt.subplots()
AAPL.plot(kind="bar", figsize=(15,6), grid=True, ax=ax)
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.get_xaxis().set_major_formatter(mdates.DateFormatter('%Y-%m'))
plt.show()
Greetings and thanks