Home > Net >  Bar plot Matplotlib : Date interval (xaxis) issue with twinx
Bar plot Matplotlib : Date interval (xaxis) issue with twinx

Time:09-16

I don't understand why my piece of code doesn't work I would like to have a xaxis an x axis with an interval of 1.

df['Dates'] = pd.to_datetime(df.Dates)

fig, ax = plt.subplots(figsize=(16, 9))
ax.bar(df['Dates'],
       df['Score'],
       color='blue',
       width=2)
date_form = DateFormatter("%d/%m/%Y")
ax.xaxis.set_major_formatter(date_form)
ax.xaxis.set_major_locator(mdates.DayLocator(interval=1))
ax2=ax.twinx()
ax2.plot(df['Dates'],
       df['Price'],
         color = 'black')
plt.show()

enter image description here

CodePudding user response:

Currently, ax's date formatting just gets overridden by ax2.plot, so you should apply the date formatter to ax2 instead of ax. Also

  • Related