Search code examples
pandasmatplotlibplotaxis-labels

Increasing Frequency of x-axis labels for dates on DataFrame plot


I have a pandas DataFrame with two columns: month_of_sale which is a date, and number_of_gizmos_sold which is a number.

I'm trying to increase the frequency of the labels on the x-axis so it's easier to read, but I can't!

Here is the df.head() of my table:

DataFrame.head()

and this is what it plots: df.plot(y='number_of_gizmos_sold', figsize=(15,5))

DataFrame.plot()

I'd like to increase the frequency of the labels, because there's a big space in between them.

What I've tried

plot.xaxis.set_major_locator(MonthLocator()) but that seems to increase the distance between the labels even more.

enter image description here

plot.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))

Strangely, I end up with this: enter image description here

The questions that last plot raises for me are:

  • What's up with 0002 as the year?
  • And why do I still have the old Jul labels there too?

Solution

  • I haven't traced the problem back to its source, but per bmu's solution, if you call ax.plot instead of df.plot, then you can configure the result using ax.xaxis.set_major_locator and ax.xaxis.set_major_formatter.

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    np.random.seed(2016)
    
    dates = pd.date_range('2013-03-01', '2016-02-01', freq='M')
    nums = (np.random.random(len(dates))-0.5).cumsum()
    df = pd.DataFrame({'months': dates, 'gizmos': nums})
    df['months'] = pd.to_datetime(df['months'])
    df = df.set_index('months')
    
    fig, ax = plt.subplots()
    ax.plot(df.index, df['gizmos'])
    # df.plot(y='gizmos', ax=ax)
    ax.xaxis.set_major_locator(mdates.MonthLocator(interval=2))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    plt.show()
    

    enter image description here