python - Matplotlib AutoDateLocator not working with DatetimeIndex -
i have dataframe datetimeindex so:
in [3]: index = pd.date_range('september 1 2014', 'september 1 2015', freq='m') ...: index out[3]: datetimeindex(['2014-09-30', '2014-10-31', '2014-11-30', '2014-12-31', '2015-01-31', '2015-02-28', '2015-03-31', '2015-04-30', '2015-05-31', '2015-06-30', '2015-07-31', '2015-08-31'], dtype='datetime64[ns]', freq='m'
plotting without changing x tick labels or explicit date formatting yields x-axis 0-12.
my figure contains 13 subplots in 1 column. i'm trying set x-axis on last plot using autodatelocator() @ end of code after subplots plotted:
fig.axes[-1].xaxis.set_major_locator(mdates.autodatelocator())
which returns following error:
valueerror: ordinal must >= 1
i tried converting dataframe index dates2num suggested here yielded same result:
df.index = mdates.date2num(df.index.to_pydatetime())
i tried consulting documentation couldn't dig other way make matplotlib recognize x-axis dates.
here complete code:
import numpy np import pandas pd pandas import series, dataframe import matplotlib mpl import matplotlib.pyplot plt import matplotlib.dates mdates import seaborn sns index = pd.date_range('september 1 2014', 'september 1 2015', freq='m') data = np.random.random([12, 13]) df = dataframe(data=data, index=index) # draw figure fig = plt.figure(figsize=(19,20), dpi=72) fig.suptitle('chart', fontsize=40, color='#333333') # draw charts in range(0, len(df)): ax = plt.subplot(len(df),1, i+1) # set ticks , labels ax.tick_params(direction='in', length=45, pad=60,colors='0.75') ax.yaxis.set_tick_params(size=0) ax.set_yticklabels([]) ax.set_ylim([0, 2]) plt.ylabel(df.columns[i], rotation=0, labelpad=60, fontsize=20, color='#404040') # remove spines sns.despine(ax=ax, bottom=true) # draw baseline, data, , threshold lines # threshold ax.axhline(1.6, color='#a0db8e', linestyle='--', label='threshold') # draw baseline ax.axhline(1, color='0.55',label='enterprise') # plot data ax.plot(df.iloc[:, i], color='#14509f',label='data') # subplot spacing fig.subplots_adjust(hspace=1) # hide tick labels , first/last tick lines plt.setp([a.get_xticklabels() in fig.axes[:-1]], visible=false) plt.setp([a.get_xticklines()[-2:] in fig.axes],visible=false) # date in x-axis fig.axes[-1].xaxis.set_major_locator(mdates.autodatelocator()) fig.axes[-1].xaxis.set_major_formatter(mdates.dateformatter('%y.%m.%d'))
Comments
Post a Comment