Python 不同子批次的matplotlib集合定位器

Python 不同子批次的matplotlib集合定位器,python,datetime,matplotlib,subplot,Python,Datetime,Matplotlib,Subplot,我每年都会整理好几份数据。我想为每个不同的年份绘制一个独特的子地块,并希望我的定位器和格式化程序在x轴上显示年份和月份 但是,我无法通过定位器和格式化程序显示不同的x轴时间范围 有没有办法把它画出来? 子标段1:2018-2019 子地块2:2019-2020 子地块3:2020-2021 我试过这样做,我想用一个for循环,以防年的范围很大 import matplotlib.pyplot as plt import matplotlib.dates as md import datetime

我每年都会整理好几份数据。我想为每个不同的年份绘制一个独特的子地块,并希望我的定位器和格式化程序在x轴上显示年份和月份

但是,我无法通过定位器和格式化程序显示不同的x轴时间范围

有没有办法把它画出来? 子标段1:2018-2019 子地块2:2019-2020 子地块3:2020-2021

我试过这样做,我想用一个for循环,以防年的范围很大

import matplotlib.pyplot as plt
import matplotlib.dates as md
import datetime
years = md.YearLocator()
years_fmt = md.DateFormatter('%Y')
months = md.MonthLocator()
month_fmt = md.DateFormatter('%b')
datax = [datetime.date(2018,10,11), datetime.date(2019,2,10), datetime.date(2020,6,6)]
datay = [1,2,3]
fig, axs = plt.subplots(len(datax), 1)
for idx, ax in enumerate(axs):

    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(years_fmt)
    ax.tick_params(which='major', axis = 'x', pad = 14)
    ax.xaxis.set_minor_locator(months)
    ax.xaxis.set_minor_formatter(month_fmt)
 
    xmin = datetime.date(datax[idx].year,1,1)
    xmax = datetime.date(datax[idx].year+1,1,1)

    ax.set_xlim(left = xmin , right = xmax)       
    ax.set_ylim(0,5)

    ax.bar(datax[idx], datay[idx], width = 5)
plt.subplots_adjust(hspace = 0.5)
以下是我得到的结果:


最好的

每个轴需要一个定位器实例,请参见:


每个轴需要一个定位器实例,请参见:


你也不是唯一一个被抓到的人。改进文档还有很多工作要做。你也不是唯一一个被发现的人。在改进文档方面还有很多需要改进的地方。
for idx, ax in enumerate(axs):
    ax.xaxis.set_major_locator(md.YearLocator())
    ax.xaxis.set_major_formatter(years_fmt)
    ax.tick_params(which='major', axis='x', pad=14)
    ax.xaxis.set_minor_locator(md.MonthLocator())
    ax.xaxis.set_minor_formatter(month_fmt)