0

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;

enter image description here

but when I uncomment the problem line in the above code I get the following figure, with X-axis labels totally changed;

enter image description here

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;

enter image description here

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
Navdeep
  • 823
  • 1
  • 16
  • 35
  • Take a look here: https://stackoverflow.com/questions/64919511/dateformatter-is-bringing-1970-as-year-not-the-original-year-in-the-dataset – prody Aug 25 '21 at 12:47
  • in your update code block, no problem exists, points in the above plot between dates that writing on the x-axis. see my new answer, maybe helps you. – I'mahdi Aug 25 '21 at 16:22

0 Answers0