[Matplotlib-devel] bar plot xtick limits with dates

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Matplotlib-devel] bar plot xtick limits with dates

Isaac Gerg
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
Inline image 1

_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: bar plot xtick limits with dates

Jody Klymak
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 <[hidden email]> 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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-devel

_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: bar plot xtick limits with dates

Isaac Gerg
No. Do I use a string with it?

Sent from my Android.

On May 1, 2017 2:50 PM, "Klymak Jody" <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-devel


_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: bar plot xtick limits with dates

Isaac Gerg
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 <[hidden email]> wrote:
No. Do I use a string with it?

Sent from my Android.

On May 1, 2017 2:50 PM, "Klymak Jody" <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-devel



_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: bar plot xtick limits with dates

Isaac Gerg
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 <[hidden email]> 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 <[hidden email]> wrote:
No. Do I use a string with it?

Sent from my Android.

On May 1, 2017 2:50 PM, "Klymak Jody" <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-devel




_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: bar plot xtick limits with dates

Jody Klymak
In reply to this post by Isaac Gerg

On 1 May 2017, at  12:22 PM, Isaac Gerg <[hidden email]> wrote:

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

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

 

--
Jody Klymak    






_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Loading...