Python 在pandas/matplotlib中格式化timeseries x轴

Python 在pandas/matplotlib中格式化timeseries x轴,python,datetime,numpy,matplotlib,Python,Datetime,Numpy,Matplotlib,我想显示每个月的缩写,以及每年的年份 我很接近。我目前面临的问题是年份不正确。我发现这是numpy.datetime64(datetime索引采用这种格式)和python datetime(1970年使用)之间的问题。图表上显示的两年应为2017年和2018年,但分别为48年和49年 import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib.dates import MonthL

我想显示每个月的缩写,以及每年的年份

我很接近。我目前面临的问题是年份不正确。我发现这是numpy.datetime64(datetime索引采用这种格式)和python datetime(1970年使用)之间的问题。图表上显示的两年应为2017年和2018年,但分别为48年和49年

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from matplotlib.dates import MonthLocator, WeekdayLocator, DateFormatter, YearLocator

indx = pd.date_range('2017-04-01', '2019-01-01')
s = pd.Series(np.random.randn(len(indx)), index=indx)

df = pd.DataFrame(s)

ax = df.plot()
months = MonthLocator(range(1, 13), bymonthday=1, interval=1)
monthsFmt = DateFormatter("%b")
years = YearLocator(1, month=4, day=1)
yrsFmt = DateFormatter("\n %y")

ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(yrsFmt)


ax.xaxis.set_minor_locator(months)
ax.xaxis.set_minor_formatter(monthsFmt)

plt.show()
我如何在这里显示正确的年份


在玩了一些游戏之后,如果您指定轴,然后在轴上绘图(而不是调用pandas plot函数),它似乎会起作用

还请注意,我将%y更改为%y,因此格式为2017/2018,而不是17/18。

Matplotlib从零开始计算年份,但从1970年开始计算。因此,您有48年、49年等。为了避免matplotlib的这种行为,您必须从pandas datetime索引日期部分获取,然后使用
%Y
描述符获取主要刻度的完整年份:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator, WeekdayLocator, DateFormatter, YearLocator

indx = pd.date_range('2017-04-01', '2019-01-01')
s = pd.Series(np.random.randn(len(indx)), index=indx.date) # get dates
df = pd.DataFrame(s)

months = MonthLocator() # MonthLocator without args set ticks for every month
monthsFmt = DateFormatter("%b")
years = YearLocator(month=4, day=1)
yrsFmt = DateFormatter("\n%Y") # correct year descriptor

ax = df.plot()
ax.xaxis.set_minor_locator(months)
ax.xaxis.set_minor_formatter(monthsFmt)
for tick in ax.xaxis.get_minor_ticks():tick.label.set_fontsize(9) 
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(yrsFmt)

plt.show()

谢谢。这对日期有效,但它使我的图例消失,并出现错误:UserWarning:找不到带标签的对象。在单个绘图上使用label='…'kwarg。有什么想法吗?我有一个ax.legend()调用,它在初始代码中起作用。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator, WeekdayLocator, DateFormatter, YearLocator

indx = pd.date_range('2017-04-01', '2019-01-01')
s = pd.Series(np.random.randn(len(indx)), index=indx.date) # get dates
df = pd.DataFrame(s)

months = MonthLocator() # MonthLocator without args set ticks for every month
monthsFmt = DateFormatter("%b")
years = YearLocator(month=4, day=1)
yrsFmt = DateFormatter("\n%Y") # correct year descriptor

ax = df.plot()
ax.xaxis.set_minor_locator(months)
ax.xaxis.set_minor_formatter(monthsFmt)
for tick in ax.xaxis.get_minor_ticks():tick.label.set_fontsize(9) 
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(yrsFmt)

plt.show()