Last answered:

06 Dec 2021

Posted on:

06 Dec 2021

0

Matplotlib graphs are not showing dates on the horizontal axis.

Matplotlib graphs are not showing dates on the horizontal axis.

1 answers ( 0 marked as helpful)
Posted on:

06 Dec 2021

0

Not seeing the dates on a graph is something that is expected to happen while using the newest versions of matplotlib. Below you can find a description of the code that will allow the dates to be displayed along the horizontal line.

First, make sure you have imported ‘pandas’ and ‘matplotlib.pyplot’ earlier in your code.

import pandas as pd

import matplotlib.pyplot as plt

Then, while creating a DataFrame (for simplicity – df ), regardless of whether you insert its content from an online API or a *.csv file, make sure there is an index column called ‘date’.

Here’s the code if you choose to import the data from a *.csv file.

df = pd.DataFrame(pd.read_csv('sample.csv', index_col='date'))

At this stage, you need to convert the date column values into "datetime" format. After you have done that, matplotlib is supposed to plot the x-axis correctly.

pd.to_datetime(df.date)

df.plot()

plt.show()

Submit an answer