I am trying to format dates on the X-axis
in the format "Month, DD YYYY" in the figure that I am creating using the following code(with the problem line of code commented);
df = pd.read_csv('data_bitcoin.csv')
price_date = df['Date']
price_close = df['Close']
plt.plot_date(price_date, price_close, linestyle = 'solid')
plt.gcf().autofmt_xdate()
date_format = mpl_dates.DateFormatter('%b, %d %Y')
#plt.gca().xaxis.set_major_formatter(date_format) <-- problem line
plt.show()
Once I execute this code I get the following figure;
but when I uncomment the problem line in the above code I get the following figure, with X-axis
labels totally changed;
What could be the problem? Why is date totally changing? I think its because of some behavior of plt.gca(
) but I am not sure. Kindly help. Thanks.
Update:
The same problem line of code is working here in this piece of code:
plt.style.use('seaborn')
dates = [
datetime(2016,1,1),
datetime(2016,1,12),
datetime(2016,1,15),
datetime(2016,1,20),
datetime(2016,1,22),
datetime(2016,1,26),
datetime(2016,1,30),
]
y = [1,2,3,4,5,6,7]
plt.plot_date(dates,y,linestyle='solid')
plt.gcf().autofmt_xdate()
date_format = mpl_dates.DateFormatter('%b, %d %Y')
plt.gca().xaxis.set_major_formatter(date_format)
plt.show()
and giving the following output;