【问题标题】:Show more data of x axis显示更多x轴数据
【发布时间】:2020-10-31 11:00:07
【问题描述】:

我有一个看起来像这样的情节:

如何增加 x 轴上显示的年数,使其看起来像这样:

如您所见,它已显示所有年份,但 matplotlib 上的默认设置并未显示所有年份。
如果我这样做了plt.xticks(data.index),我仍然没有看到这些年。

我尝试了_=plt.xticks(ticks=range(1970,2020,2)),如答案中所述,但我得到了以下图表。

人口是这样的:

population
Out[49]: 
1960-01-01    180671000.0
1961-01-01    183691000.0
1962-01-01    186538000.0
1963-01-01    189242000.0
1964-01-01    191889000.0
1965-01-01    194303000.0
1966-01-01    196560000.0
1967-01-01    198712000.0
1968-01-01    200706000.0
1969-01-01    202677000.0
1970-01-01    205052000.0
1971-01-01    207661000.0
1972-01-01    209896000.0
1973-01-01    211909000.0
1974-01-01    213854000.0
1975-01-01    215973000.0
1976-01-01    218035000.0
1977-01-01    220239000.0
1978-01-01    222585000.0
1979-01-01    225055000.0
1980-01-01    227225000.0
1981-01-01    229466000.0
1982-01-01    231664000.0
1983-01-01    233792000.0
1984-01-01    235825000.0
1985-01-01    237924000.0
1986-01-01    240133000.0
1987-01-01    242289000.0
1988-01-01    244499000.0
1989-01-01    246819000.0
1990-01-01    249623000.0
1991-01-01    252981000.0
1992-01-01    256514000.0
1993-01-01    259919000.0
1994-01-01    263126000.0
1995-01-01    266278000.0
1996-01-01    269394000.0
1997-01-01    272657000.0
1998-01-01    275854000.0
1999-01-01    279040000.0
2000-01-01    282162411.0
2001-01-01    284968955.0
2002-01-01    287625193.0
2003-01-01    290107933.0
2004-01-01    292805298.0
2005-01-01    295516599.0
2006-01-01    298379912.0
2007-01-01    301231207.0
2008-01-01    304093966.0
2009-01-01    306771529.0
2010-01-01    309321666.0
2011-01-01    311556874.0
2012-01-01    313830990.0
2013-01-01    315993715.0
2014-01-01    318301008.0
2015-01-01    320635163.0
2016-01-01    322941311.0
2017-01-01    324985539.0
2018-01-01    326687501.0
2019-01-01    328239523.0
dtype: float64

【问题讨论】:

    标签: python datetime matplotlib formatting xticks


    【解决方案1】:

    您可能希望明确设置刻度值的范围,例如:

    _=plt.xticks(ticks=range(1970,2020,2))
    

    【讨论】:

    • 我有population.plot(rot =90) 你能解释一下_ 的意思吗
    • _= 是用来隐藏 xticks 函数的输出的,检查一下:stackoverflow.com/questions/12056115/… 如果这解决了您的问题,请接受答案。
    【解决方案2】:

    我假设您正在从 data.csv 文件加载数据,其中有两列:timepopulation。您可以加载数据并将time 列格式设置为日期时间。然后你可以绘制population;最后,您可以通过ax.xaxis.set_major_locatorax.xaxis.set_major_formatter 方法调整xaxis 刻度。检查此代码作为参考:

    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.dates as md
    
    df = pd.read_csv('data.csv')
    df.set_index('time', inplace = True)
    df.index = pd.to_datetime(df.index, format = '%Y-%m-%d')
    
    fig, ax = plt.subplots(figsize = (10, 5))
    
    ax.plot(df['population'])
    
    ax.xaxis.set_major_locator(md.YearLocator())
    ax.xaxis.set_major_formatter(md.DateFormatter('%Y'))
    plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90)
    ax.set_xlim([df.index[0], df.index[-1]])
    
    plt.show()
    

    这给了我这个情节:

    与:

    • ax.xaxis.set_major_locator(md.YearLocator()) 我告诉 matplotlib 每年打勾
    • ax.xaxis.set_major_formatter(md.DateFormatter('%Y')) 我告诉 matplotlib 每个刻度只写年份,忽略月份和日期
    • plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90) 我告诉metplotlib 将刻度标签旋转90°
    • ax.set_xlim([df.index[0], df.index[-1]])我告诉 matplotlib 修复情节的末端

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 2019-09-05
      • 1970-01-01
      相关资源
      最近更新 更多