Last active
February 1, 2021 02:34
-
-
Save robdmc/e25318527cd3d472f9b7 to your computer and use it in GitHub Desktop.
matplotlib date format
This file contains 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
# Here is a simple version | |
def make_plots_simple(): | |
import pandas as pd | |
import pylab as pl | |
import matplotlib.dates as mdates | |
x = pd.date_range('4/1/2020', '4/20/2020', freq='H') | |
y = [n for n in range(len(x))] | |
fig, ax = pl.subplots() | |
day_fmt = mdates.DateFormatter('%Y-%m-%d') | |
ax.xaxis.set_major_formatter(day_fmt) | |
fig.autofmt_xdate() | |
ax.plot(x, y) | |
# this still needs work, I just put what I had here to remind myself that I need to do this | |
def make_plots(*args, **kwargs): | |
x = [d.to_datetime() for d in args[0]] | |
pl.plot(x, *args[1:], **kwargs) | |
fig, ax = pl.gcf(), pl.gca() | |
fig.autofmt_xdate(bottom=0.2, rotation=30, ha=u'right') | |
mondays = dates.WeekdayLocator(dates.MONDAY) | |
months = MonthLocator(range(1, 13), bymonthday=1, interval=1) | |
monthsFmt = dates.DateFormatter("%a %b %d'%y") | |
ax.xaxis.set_major_locator(mondays) | |
ax.xaxis.set_major_formatter(monthsFmt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment