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]:
dates values
0 2000-10-01 0.000000
1 2000-10-02 1.000000
2 2000-10-03 1.414214
3 2000-10-04 1.732051
4 2000-10-05 2.000000

5 rows × 2 columns


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]:
<matplotlib.axes.AxesSubplot at 0x7f7aa6961ad0>

In [5]:
# or set the dates as an index
df = df.set_index('dates')
# and then plot the series
df['values'].plot()


Out[5]:
<matplotlib.axes.AxesSubplot at 0x7f7aa684d090>

In [5]:


In [5]: