You should use a secondary axis for the year
labels, while using the primary for the month
label. You can generate a secondary axis with something like:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.dates as md
fig = plt.figure()
months = host_subplot(111, axes_class = AA.Axes, figure = fig)
plt.subplots_adjust(bottom = 0.1)
years = months.twiny()
Then you should move the secondary axis to the bottom with:
offset = -20
new_fixed_axis = years.get_grid_helper().new_fixed_axis
years.axis['bottom'] = new_fixed_axis(loc = 'bottom',
axes = years,
offset = (0, offset))
Finally you plot and then adjust the primary and secondary axes format with md.MonthLocator
and md.YearLocator
. Something like this should be fine:
months.xaxis.set_major_locator(md.MonthLocator(interval = 1))
months.xaxis.set_major_formatter(md.DateFormatter('%B'))
months.set_xlim([df['Date'].iloc[0], df['Date'].iloc[-1]])
years.xaxis.set_major_locator(md.YearLocator(interval = 1))
years.xaxis.set_major_formatter(md.DateFormatter('%H'))
years.set_xlim([df['Date'].iloc[0], df['Date'].iloc[-1]])
Try to check these two answers, it should not be difficult to adapt those codes to your case:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…