1 回答

TA貢獻(xiàn)2019條經(jīng)驗 獲得超9個贊
您應(yīng)該為標(biāo)簽使用輔助軸year,同時為標(biāo)簽使用主要軸month。您可以使用以下內(nèi)容生成輔助軸:
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()
然后你應(yīng)該將輔助軸移動到底部:
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))
最后,您繪制然后使用 和 調(diào)整主軸和次軸md.MonthLocator格式md.YearLocator。這樣的事情應(yīng)該沒問題:
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]])
添加回答
舉報