Skip to content

Instantly share code, notes, and snippets.

@DGrady
Created August 18, 2017 19:27
Show Gist options
  • Save DGrady/92ae24551cb190bc2840df36cd76d0db to your computer and use it in GitHub Desktop.
Save DGrady/92ae24551cb190bc2840df36cd76d0db to your computer and use it in GitHub Desktop.
Plot Pandas date times with matplotlib
"""
Most of the time, it’s pretty easy to take a Pandas data frame that contains
timestamps and plot it, for example using `df.plot(…)`. Nowadays many of
high-level Matplotlib functions are canny enough to handle Pandas timestamps on
their own as well.
But not all of them.
So how do you get nice handling of Pandas timestamps when you need to use
Matplotlib directly?
"""
from datetime import datetime
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
f, ax = plt.subplots(figsize=(8, 2))
hb = plt.hexbin(
mpl.dates.date2num(df.date.astype(datetime)),
df.y,
)
ax.xaxis_date()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment