[Matplotlib-devel] bar plot xtick limits with dates
Klymak Jody
jklymak at uvic.ca
Mon May 1 14:50:31 EDT 2017
Did you try ax.set_xlim after your tick locator calls?
Cheers. Jody.
Sent from my iPhone
> On May 1, 2017, at 11:29, Isaac Gerg <isaac.gerg at gergltd.com> wrote:
>
> Hi all,
>
> I am trying to make a plot from a pandas dataframe and i cannot for the life of me get the xlabels to stay within range of the data. Any suggestions?
>
> attached is the plot in question. The code is:
>
> plt.figure()
> fig = matplotlib.pyplot.gcf()
> fig.set_size_inches(16,9)
> plt.title('Number Abnormal Events Per Week By Type')
> plt.bar(tmp2.index, tmp2['loose'], color='C1', width=4)
> plt.bar(tmp2.index, tmp2['hard'], color='C2', width=4, bottom=tmp2['loose'])
> plt.ylabel('Count')
> plt.xlabel('Week')
> plt.legend(['l','h'])
> plt.xticks(rotation='vertical')
> ax = matplotlib.pyplot.gca()
> ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%Y-%m-%d'))
> ax.xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=mdates.MO))
> plt.savefig(r'output\abnormal.png', bbox='tight')
>
> with
>
> tmp2.index
> DatetimeIndex(['2017-01-02', '2017-01-09', '2017-01-16', '2017-01-23',
> '2017-01-30', '2017-02-06', '2017-02-13', '2017-02-20',
> '2017-02-27', '2017-03-06', '2017-03-13', '2017-03-20',
> '2017-03-27', '2017-04-03', '2017-04-10', '2017-04-17',
> '2017-04-24'],
> dtype='datetime64[ns]', name='datetime', freq='W-MON')
>
>
> I would like to remove the xlabels on either end of the data (12-26 and 05-01).
>
> Thanks,
> Isaac
> <abnormal.png>
> _______________________________________________
> Matplotlib-devel mailing list
> Matplotlib-devel at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-devel
More information about the Matplotlib-devel
mailing list