Datetime object in line graph

Screen Link: https://app.dataquest.io/m/143/multiple-plots/1/recap

Your Code: Enclose your code in 3 backticks like this to format properly:

import pandas as pd
import matplotlib.pyplot as plt

unrate = pd.read_csv('unrate.csv')
unrate['DATE'] = pd.to_datetime(unrate['DATE'])
first_twelve = unrate[0:12]
plt.plot(first_twelve["DATE"], first_twelve["VALUE"])
plt.xticks(rotation = 90)
plt.xlabel("Month")
plt.ylabel("Unemployment Rate")
plt.title("Monthly Unemployment Trends, 1948")```

I expected that a graph with x ticks labels in format date-month-year. But, the graph had x ticks labels as the name of the month followed by the year, e.g. Jan 1964.
How does this work?
Why the date of that x tick label is not showing?

This is a really great question, and I personally never questioned the results!

I believe it has to do with how matplotlib handles datetime objects. Looking at some matplotlib documentation, I found that “matplotlib allows you to natively plots python datetime instances, and for the most part does a good job picking tick locations and string formats” [source]. Another version of the matplotlib documentation states that “the conversion, tick locating and formatting is done behind the scenes so this is most transparent to you.”

Just because I was curious, I used Jupyter notebook and worked locally to see how the graph would look. The dates were formatted like this:
image

It’s possible that either Dataquest has adjusted the settings in the background for the mission so that matplotlib would write the dates as we see in the solution. It’s also possible that the version of matplotlib on my PC is different and has a different default. In any case, it seems clear that it’s something happening with matplotlib using datetime objects behind the scenes. I’m pretty sure there’s ways to tweak it if you have a look through the documentation.

Thanks for the interesting question!

3 Likes