Matplotlib年份作为年末的绘图

Matplotlib年份作为年末的绘图,matplotlib,Matplotlib,我的图表底部有年份,如下所示 我有每日数据和年度数据。年度数据报告为年末数据 我如何把1990年变成1989-12年 我曾经尝试过这样的monkey修补DateFormatter: my_format = mdates.DateFormatter('%Y') def new_caller(self, x, pos=0): print('called date formatter') d = pd.Timestamp(num2date(x, self.tz)) - pd.Dat

我的图表底部有年份,如下所示

我有每日数据和年度数据。年度数据报告为年末数据

我如何把1990年变成1989-12年

我曾经尝试过这样的monkey修补DateFormatter:

my_format = mdates.DateFormatter('%Y')

def new_caller(self, x, pos=0):
    print('called date formatter')
    d = pd.Timestamp(num2date(x, self.tz)) - pd.DateOffset(years=1)
    return d.strftime(self.fmt)

my_format.__call__ = new_caller

...

ax.xaxis.set_major_formatter(my_format)

这不起作用,因为我在控制台中没有看到被称为日期格式化程序的
,所以我认为甚至没有调用日期格式化程序。如果是这样做的话,实际需要哪种方法进行修补才能使其工作?

对于包含图表的轴:

import matplotlib.dates as mdates

ax.xaxis.set_major_locator(mdates.YearLocator(base=2, month=12, day=31))
ax.xaxis.set_minor_locator(mdates.YearLocator(base=1, month=12, day=31))
根据需要对不同的碱基进行必要的修改,以及哪些蜱虫需要移动

要强制将格式设置为仅包含年份,请执行以下操作:

ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))

您需要将格式化程序附加到轴。完整的工作示例可能会有所帮助。我不完全确定你想要实现什么;你想把1990年1月1日的勾号标记为“12-1989”吗?或者你想把时间定在1989年12月1日?因为对我来说,打勾12-1989意味着1989年12月1日。格式化程序已经连接到轴。我以另一种方式解决了这个问题,通过如下设置定位器:
ax.xaxis.set_major_locator(mdates.YearLocator(base=2,month=12,day=31));ax.xaxis.set_minor_locator(mdates.YearLocator(base=1,month=12,day=31))
Great。这就是为什么我问你想把虱子放在哪里