In [1]:
import datetime
import pandas
import matplotlib.pyplot as plt
import matplotlib.dates
In [2]:
n = 100
# generate a timeseries of 100 days
dates = [datetime.datetime(2000,10,1) + datetime.timedelta(days=i) for i in range(n)]
values = [x**0.5 for x in range(n)]
df = pandas.DataFrame(data=dict(dates=dates, values=values))
df.head()
Out[2]:
In [3]:
# This is the manual apprach
# create a plot
fig, ax = plt.subplots()
# Create a month locator, creating tick marks at every month
locator = matplotlib.dates.MonthLocator()
# Create a month formatter using year-month notation
formatter = matplotlib.dates.DateFormatter('%Y-%m')
# Add the dates as numbers
ax.plot(matplotlib.dates.date2num(df['dates']), df['values'])
# And format them using the formatter
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
In [4]:
# Or simpler:
df.plot(x='dates', y='values')
# notice the combined week/month grid
Out[4]:
In [5]:
# or set the dates as an index
df = df.set_index('dates')
# and then plot the series
df['values'].plot()
Out[5]:
In [5]:
In [5]: