0

I have a plot_graph() function that plots pandas dataframe as a line chart.

def plot_graph(df):
 ax = plt.gca()
 #df["Date"].dt.strftime("%m/%d/%y")
 #df["date"] = df["date"].astype('datetime64[ns]')
 print(df['date'])
 df.plot(kind='line', x='date', y='Actual', ax=ax)
 df.plot(kind='line', x='date', y='Expected', color='red', ax=ax)
 ax.xaxis.set_major_locator(plt.MaxNLocator(3))
 plt.savefig("fig1.png")

I pass pandas dataframe in this format

 date       actual   expected
 2019-11    20       65
 2019-12    35       65

When I plot the line chart, x axis labels does not get displayed correctly as in (yyyy-mm) format. I believe it is with the date format. So I tried converting it to date. I tried with all the options(commented in the code), nothing seems to work. Any suggestions would be appreicated.

user3447653
  • 3,968
  • 12
  • 58
  • 100

2 Answers2

2

Try this:

import pandas as pd
import matplotlib.dates as mdates

def plot_graph(df):
    ax = plt.gca()
    df['date'] = pd.to_datetime(df['date']).dt.date
    df.plot(kind='line', x='date', y='actual', ax=ax)
    df.plot(kind='line', x='date', y='expected', color='red', ax=ax)
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    # ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m')) #to explicitly set format
    
plot_graph(df)

enter image description here

I think using matplotlib.dates is the best thing here, but it seems like df.plot() needs dates to be date and not datetime (or string). If you instead plot directly through matplotlib you don't need to do this. More here.

Tom
  • 8,310
  • 2
  • 16
  • 36
1
import pandas as pd
import numpy as np  # for test data
from datetime import datetime  # for test data
import matplotlib.dates as mdates
import matplotlib.pyplot as plt

# synthetic data with date as a datetime
np.random.seed(365)
length = 700
df = pd.DataFrame(np.random.rand(length, 2) * 10, columns=['Actual', 'Expected'], index=pd.bdate_range(datetime.today(), freq='d', periods=length).tolist()).reset_index()

# display(df.head())
       index    Actual  Expected
0 2020-07-16  9.414557  6.416027
1 2020-07-17  6.846105  5.885621
2 2020-07-18  5.438872  3.680709
3 2020-07-19  7.666258  3.050124
4 2020-07-20  4.420860  1.104433


# function
def plot_graph(df):
    
    #  df.date = pd.to_datetime(df.date)  # if needed and date is the column name

    fig, ax = plt.subplots()
    
    months = mdates.MonthLocator()  # every month
    months_fmt = mdates.DateFormatter('%Y-%m')   # format

    ax.plot('index', 'Actual', data=df)
    ax.plot('index', 'Expected', data=df, color='red')

    # format the ticks
    ax.xaxis.set_major_locator(months)
    ax.xaxis.set_major_formatter(months_fmt)
    
    plt.xticks(rotation=90)
    
    plt.legend()

    plt.show()


plot_graph(df)

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158