pythondatematplotlibaxis

Data apparently plotted wrong way on matplotlib


I am plotting a graph with date on the x axis and data on the y axis. However the graph is completly wrong and I don't understand why...

df['Date_TrailClean'] = pd.to_datetime(df['Date_TrailClean'])
# x axis values
x = df['Date_TrailClean']
# corresponding y axis values
y = df['AdjTotItems']

fig, ax = plt.subplots()
# plotting the points 
ax.plot(x, y)

ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=(1, 7)))
ax.xaxis.set_minor_locator(mdates.MonthLocator())

ax.set_ylabel(r'Adjusted Total Items')

# function to show the plot
plt.show()

Which produces a graph like this, as if the data is plotted on the wrong axes?

enter image description here

Data can be accessed here: https://docs.google.com/spreadsheets/d/10bf0dEUz5lvBjh4kWH8XXfX0yOzdU8NYjSzQBwNV4bk/edit?usp=sharing


Solution

  • import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    
    
    df = pd.read_excel('sample_data.xlsx')
    df['Date_TrailClean'] = pd.to_datetime(df['Date_TrailClean'])
    
    # Sort x-axis 
    df = df.sort_values('Date_TrailClean')
    x = df['Date_TrailClean']
    y = df['AdjTotItems']
    
    
    fig, ax = plt.subplots(figsize=(10, 6))
    ax.plot(x, y, color='blue', marker='o', label='Adjusted Total Items')
    ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=(1, 7)))
    ax.xaxis.set_minor_locator(mdates.MonthLocator())
    
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))  # e.g., Jan 2025, Jul 2025
    
    plt.xticks(rotation=45)
    
    ax.set_xlabel('Date')
    ax.set_ylabel(r'Adjusted Total Items')
    ax.set_title('Adjusted Total Items Over Time')
    ax.grid(True)
    plt.tight_layout()  
    plt.show()
    
    

    plot

    As others pointed out, sorting the x-axis data gives you above plot.