Created
August 18, 2017 19:27
-
-
Save DGrady/92ae24551cb190bc2840df36cd76d0db to your computer and use it in GitHub Desktop.
Plot Pandas date times with matplotlib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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