1 year ago

#296097

test-img

Piyush Ghasiya

How to customise date range in time series matplotlib?

I have a data for two years and I am using below code to create a time series graph:

import matplotlib.pyplot as plt

df = pd.read_csv("Sample.csv")
# Draw Plot
def plot_df(df, x, y, title="", xlabel='Date', ylabel='Number', dpi=100):
    plt.figure(figsize=(16,5), dpi=dpi)
    plt.plot(x, y, color='tab:red')
    plt.gca().set(title=title, xlabel=xlabel, ylabel=ylabel)
    plt.savefig("Sample Figure.png")
    plt.show()


plot_df(df, x=df.created_at, y=df.text, title='Sample Figure')

This code produced below plot:

Time Series Plot

As you can see the x axis (Date) is not visible.

What changes should I apply in the above code so that I can customise the date interval? I know from several tutorials and stack questions that it can be done using MonthLocator (in my case as I want monthly interval) but I can't apply that successfully in this code.

Thanks in Advance!!

python

matplotlib

time-series

xticks

0 Answers

Your Answer

Accepted video resources