Here's a simplified version of the code which explains what I'm trying to do:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dates = pd.date_range('20070101',periods=1000)
df = pd.DataFrame(np.random.randn(1000), index = dates, columns =list ('A'))
plt.plot(df['A'])
this results in this graph:
I want to use the Year from the datetime index as the labels for the x axis on this graph, not the number of datapoints/days. I want 2007, 2008, 2009 etc based on the datetime index (as this will vary according to my input data).
I've looked at every help site for this and nothing seems to work, I may be missing something very obvious, which I apologise for, but I can't figure this out.
EDIT
New code to illustrate error:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
dates = pd.date_range('20070101',periods=1000)
df = pd.DataFrame(np.random.randn(1000), columns =list ('A'))
df['date'] = dates
def get_season(row):
if row['date'].month >= 3 and row['date'].month <= 5:
return 'spring'
elif row['date'].month >= 6 and row['date'].month <= 8:
return 'summer'
elif row['date'].month >= 9 and row['date'].month <= 11:
return 'autumn'
else:
return 'winter'
df['Season'] = df.apply(get_season, axis=1)
df['Year'] = df['date'].dt.year
df.loc[df['date'].dt.month == 12, 'Year'] += 1
df = df.set_index(['Year', 'Season'], inplace=False)
df.head()
fig,ax = plt.subplots()
df.plot(x_compat=True,ax=ax)
ax.xaxis.set_tick_params(reset=True)
ax.xaxis.set_major_locator(mdates.YearLocator(1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.show()
This gives the error:
ValueError: ordinal must be >= 1
This seems to come from the line
ax.xaxis.set_major_locator(mdates.YearLocator(1))
I think it's to do with the multi-index, but don't understand how to plot it with the multi-index.


