Search code examples
pythondatetimematplotlibformattingxticks

Show more data of x axis


I have a plot which looks like this:

enter image description here

How can I increase the number of years shown in the x axis so that it looks like this:

enter image description here

As you can see it has shown all the years but the default setting on matplotlib does not show all the years.
If I do plt.xticks(data.index) I still don't see all the years.

enter image description here

I tried _=plt.xticks(ticks=range(1970,2020,2)) as mentioned in the answer but I am getting the following chart.

enter image description here

Population is this:

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

Solution

  • I suppose you are loading your data from a data.csv file, where you have two columns: time and population. You can load your data and set the time column format do datetime. Then you can plot the population; finally you can adjust the xaxis ticks through ax.xaxis.set_major_locator and ax.xaxis.set_major_formatter methods. Check this code as a reference:

    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()
    

    which gives me this plot:

    enter image description here

    With:

    • ax.xaxis.set_major_locator(md.YearLocator()) I tell matplotlib to put a tick per year
    • ax.xaxis.set_major_formatter(md.DateFormatter('%Y')) I tell matplotlib to write only the year for each tick, neglecting month and day
    • plt.setp(ax.xaxis.get_majorticklabels(), rotation = 90) I tell metplotlib to rotate the tick labels by 90°
    • ax.set_xlim([df.index[0], df.index[-1]]) I tell matplotlib to fix the extremities of the plot