Plot the x-axis as a date

Set the index as a datetime dtype

If you set the index to the datetime series by converting the dates with pd.to_datetime(...), matplotlib will handle the x axis for you.

Here is a minimal example of how you might deal with this visualization.

Plot directly with pandas.DataFrame.plot, which uses matplotlib as the default backend.

Simple example:

import pandas as pd
import matplotlib.pyplot as plt

date_time = ["2011-09-01", "2011-08-01", "2011-07-01", "2011-06-01", "2011-05-01"]

# convert the list of strings to a datetime and .date will remove the time component
date_time = pd.to_datetime(date_time).date
temp = [2, 4, 6, 4, 6]

DF = pd.DataFrame({'temp': temp}, index=date_time)

ax = DF.plot(x_compat=True, rot=90, figsize=(6, 5))

This will yield a plot that looks like the following:

enter image description here

Setting the index makes things easier

The important note is that setting the DataFrame index to the datetime series allows matplotlib to deal with x axis on time series data without much help.

Follow this link for detailed explanation on spacing axis ticks (specifically dates)

Leave a Comment