[Matplotlib-devel] bar plot xtick limits with dates

Isaac Gerg isaac.gerg at gergltd.com
Mon May 1 14:29:37 EDT 2017


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
[image: Inline image 1]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20170501/a1a5250c/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: abnormal.png
Type: image/png
Size: 203167 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20170501/a1a5250c/attachment-0001.png>


More information about the Matplotlib-devel mailing list