Python 如何在x轴测线图上显示年份?

Python 如何在x轴测线图上显示年份?,python,python-3.x,pandas,matplotlib,jupyter,Python,Python 3.x,Pandas,Matplotlib,Jupyter,我正在尝试使用pandas和matplotlib绘制一些数据 考虑以下数据: years = [i for i in range(2005, 2016)] values = [49.929266640000002, 45.441518010000003, 49.762879810000001, 52.849612180000001, 57.618790150000002, 47.750615240000002, 47.508212309999998, 37.414841590000002, 45

我正在尝试使用
pandas
matplotlib
绘制一些数据

考虑以下数据:

years = [i for i in range(2005, 2016)]
values = [49.929266640000002, 45.441518010000003, 49.762879810000001, 52.849612180000001, 57.618790150000002, 47.750615240000002, 47.508212309999998, 37.414841590000002, 45.441518010000003, 47.750615240000002, 49.929266640000002]
years = [i for i in range(2008, 2016)]
values = [49.929266640000002, 45.441518010000003, 49.762879810000001, 52.849612180000001, 57.618790150000002, 47.750615240000002, 47.508212309999998, 37.414841590000002]
lineplt = pandas.Series(values, name='Some city 2008 - 2015')
lineplt.index = years
lineplt.plot(title = 'Rate some city 2008 - 2015', legend=True)
如果我绘制数据:

lineplt = pandas.Series(values, name='Some City 2005 - 2015')
lineplt.index = years
lineplt.plot(title = 'Rate some city 2005 - 2015', legend=True)
一切正常,如图所示:

但是,如果尝试小于11年的时间段
x轴
不显示年份:

例如,考虑这些数据:

years = [i for i in range(2005, 2016)]
values = [49.929266640000002, 45.441518010000003, 49.762879810000001, 52.849612180000001, 57.618790150000002, 47.750615240000002, 47.508212309999998, 37.414841590000002, 45.441518010000003, 47.750615240000002, 49.929266640000002]
years = [i for i in range(2008, 2016)]
values = [49.929266640000002, 45.441518010000003, 49.762879810000001, 52.849612180000001, 57.618790150000002, 47.750615240000002, 47.508212309999998, 37.414841590000002]
lineplt = pandas.Series(values, name='Some city 2008 - 2015')
lineplt.index = years
lineplt.plot(title = 'Rate some city 2008 - 2015', legend=True)
它表明:

是否有一个解决方案,可能是我缺少的一些参数或参数,以便显示从2008年到2015年的年份,而不是指数作为单个数字


提前谢谢

解决问题的最简单方法是显式使用
datetime
对象作为索引。Pandas为此提供了一个实用程序:

years = pd.date_range('2008','2015', freq='AS')

freq
参数采用偏移量。您可以创建自己的或使用其中一个。

您应该使用以下内容:

fig = plt.figure()                                                               
ax = fig.add_subplot(1,1,1)    

ax.get_xaxis().get_major_formatter().set_useOffset(False)
ax.set_xlim((2008, 2016))

谢谢这很容易。我制作了:
lineplt.index=pandas.date\u范围('2008','2015',freq='AS')
,效果非常好@estebanpdl是的,我经常提到那个链接。谢谢,Vasya。这一次我会选择第一个答案。就一行。