bar plot xtick limits with dates

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>

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
Matplotlib-devel Info Page

No. Do I use a string with it?

···

Sent from my Android.

On May 1, 2017 2:50 PM, "Klymak Jody" <jklymak at uvic.ca> wrote:

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
> Matplotlib-devel Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20170501/c16c695d/attachment.html&gt;

A simple string with the date in it as listed on the x-axis in the image
worked -- thank you!

···

On Mon, May 1, 2017 at 2:58 PM, Isaac Gerg <isaac.gerg at gergltd.com> wrote:

No. Do I use a string with it?

Sent from my Android.

On May 1, 2017 2:50 PM, "Klymak Jody" <jklymak at uvic.ca> wrote:

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
> Matplotlib-devel Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20170501/e76123e4/attachment-0001.html&gt;

i eventually settled on set_xlim(tmp2.index[0], tmp2.index[-1])

Why would matplotlib go out as far as it did in the first place?

···

On Mon, May 1, 2017 at 3:22 PM, Isaac Gerg <isaac.gerg at gergltd.com> wrote:

A simple string with the date in it as listed on the x-axis in the image
worked -- thank you!

On Mon, May 1, 2017 at 2:58 PM, Isaac Gerg <isaac.gerg at gergltd.com> wrote:

No. Do I use a string with it?

Sent from my Android.

On May 1, 2017 2:50 PM, "Klymak Jody" <jklymak at uvic.ca> wrote:

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
> Matplotlib-devel Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20170501/7da26fa3/attachment.html&gt;

Oh, thats fancy, I didn?t know xlim would take a string. Glad it works.

I was just trying to remind myself how to convert from a string to datetime64[ns]. Usually xlim is entered as a number?

Cheers, Jody

···

On 1 May 2017, at 12:22 PM, Isaac Gerg <isaac.gerg at gergltd.com> wrote:

A simple string with the date in it as listed on the x-axis in the image worked -- thank you!

--
Jody Klymak

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20170501/7ecf8eb0/attachment.html&gt;