changing labeling of axes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

changing labeling of axes

Slavin, Jonathan
Hi,

I'm wondering if anyone knows of a good way to relabel axes for a particular case I'm dealing with. I'm making a 2D histogram, using hist2d of course.  The thing is that for my case, where I'm plotting the density vs. pressure for results from a hydro run, it makes more sense to use the log of the quantities as the inputs for hist2d.  So then I need to give the log of the density and pressure to hist2d. This works fine for making the histogram, but then the axes are labelled with log10(n), log10(P).  I'd prefer to have the axes labelled as for a logarithmic plot, so instead of say 4.0, 10^4.  Is there a simple way of doing that. I'm pretty sure I can just specify by hand the ticks and tick labels, though that's kind of a pain. Any help would be appreciated.

Regards,
Jon

--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: (617) 496-7981       Cambridge, MA 02138-1516
cell: (781) 363-0035             USA
________________________________________________________


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

Re: changing labeling of axes

Benjamin Root
You can do `ax.set_xscale('log')` to set the x-axis to log scale and similarly for the y-axis. It'll automatically make the tick labels make sense. Also, you won't need to pass in the log10 of n or P, matplotlib will handle all of that for you.

Cheers!
Ben Root


On Wed, Nov 8, 2017 at 10:10 AM, Slavin, Jonathan <[hidden email]> wrote:
Hi,

I'm wondering if anyone knows of a good way to relabel axes for a particular case I'm dealing with. I'm making a 2D histogram, using hist2d of course.  The thing is that for my case, where I'm plotting the density vs. pressure for results from a hydro run, it makes more sense to use the log of the quantities as the inputs for hist2d.  So then I need to give the log of the density and pressure to hist2d. This works fine for making the histogram, but then the axes are labelled with log10(n), log10(P).  I'd prefer to have the axes labelled as for a logarithmic plot, so instead of say 4.0, 10^4.  Is there a simple way of doing that. I'm pretty sure I can just specify by hand the ticks and tick labels, though that's kind of a pain. Any help would be appreciated.

Regards,
Jon

--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________


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



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

Re: changing labeling of axes

Slavin, Jonathan
In reply to this post by Slavin, Jonathan
Hi Ben,

The issue with that is that I want to retain the logarithmic binning. If I change the xscale/yscale to log with the binning as set up now, then it will try to scale the log values logarithmically.  So I still want the axes to be scaled in the same way, I just want the labeling of the axes to be different.  Is that clear?

Jon

On Wed, Nov 8, 2017 at 10:25 AM, Benjamin Root <[hidden email]> wrote:
You can do `ax.set_xscale('log')` to set the x-axis to log scale and similarly for the y-axis. It'll automatically make the tick labels make sense. Also, you won't need to pass in the log10 of n or P, matplotlib will handle all of that for you.

Cheers!
Ben Root


On Wed, Nov 8, 2017 at 10:10 AM, Slavin, Jonathan <[hidden email]> wrote:
Hi,

I'm wondering if anyone knows of a good way to relabel axes for a particular case I'm dealing with. I'm making a 2D histogram, using hist2d of course.  The thing is that for my case, where I'm plotting the density vs. pressure for results from a hydro run, it makes more sense to use the log of the quantities as the inputs for hist2d.  So then I need to give the log of the density and pressure to hist2d. This works fine for making the histogram, but then the axes are labelled with log10(n), log10(P).  I'd prefer to have the axes labelled as for a logarithmic plot, so instead of say 4.0, 10^4.  Is there a simple way of doing that. I'm pretty sure I can just specify by hand the ticks and tick labels, though that's kind of a pain. Any help would be appreciated.

Regards,
Jon

--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________


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





--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: (617) 496-7981       Cambridge, MA 02138-1516
cell: (781) 363-0035             USA
________________________________________________________


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

Re: changing labeling of axes

Benjamin Root
Ah, then you need to simply change the tick formatter. Here are some examples:

https://matplotlib.org/devdocs/gallery/ticks_and_spines/tick-formatters.html

Cheers!
Ben Root


On Wed, Nov 8, 2017 at 11:09 AM, Slavin, Jonathan <[hidden email]> wrote:
Hi Ben,

The issue with that is that I want to retain the logarithmic binning. If I change the xscale/yscale to log with the binning as set up now, then it will try to scale the log values logarithmically.  So I still want the axes to be scaled in the same way, I just want the labeling of the axes to be different.  Is that clear?

Jon

On Wed, Nov 8, 2017 at 10:25 AM, Benjamin Root <[hidden email]> wrote:
You can do `ax.set_xscale('log')` to set the x-axis to log scale and similarly for the y-axis. It'll automatically make the tick labels make sense. Also, you won't need to pass in the log10 of n or P, matplotlib will handle all of that for you.

Cheers!
Ben Root


On Wed, Nov 8, 2017 at 10:10 AM, Slavin, Jonathan <[hidden email]> wrote:
Hi,

I'm wondering if anyone knows of a good way to relabel axes for a particular case I'm dealing with. I'm making a 2D histogram, using hist2d of course.  The thing is that for my case, where I'm plotting the density vs. pressure for results from a hydro run, it makes more sense to use the log of the quantities as the inputs for hist2d.  So then I need to give the log of the density and pressure to hist2d. This works fine for making the histogram, but then the axes are labelled with log10(n), log10(P).  I'd prefer to have the axes labelled as for a logarithmic plot, so instead of say 4.0, 10^4.  Is there a simple way of doing that. I'm pretty sure I can just specify by hand the ticks and tick labels, though that's kind of a pain. Any help would be appreciated.

Regards,
Jon

--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________


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





--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________



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

Re: changing labeling of axes

tcaswell
In reply to this post by Slavin, Jonathan
You can do this with the formatter on the axis, see https://matplotlib.org/gallery/ticks_and_spines/tick-formatters.html#sphx-glr-gallery-ticks-and-spines-tick-formatters-py and https://matplotlib.org/api/ticker_api.html

You can also pass log-spaced bins to `hist2D` and then set the axis scales to log (but trying to construct an example did not got as I expected...)

Tom

On Wed, Nov 8, 2017 at 10:10 AM Slavin, Jonathan <[hidden email]> wrote:
Hi,

I'm wondering if anyone knows of a good way to relabel axes for a particular case I'm dealing with. I'm making a 2D histogram, using hist2d of course.  The thing is that for my case, where I'm plotting the density vs. pressure for results from a hydro run, it makes more sense to use the log of the quantities as the inputs for hist2d.  So then I need to give the log of the density and pressure to hist2d. This works fine for making the histogram, but then the axes are labelled with log10(n), log10(P).  I'd prefer to have the axes labelled as for a logarithmic plot, so instead of say 4.0, 10^4.  Is there a simple way of doing that. I'm pretty sure I can just specify by hand the ticks and tick labels, though that's kind of a pain. Any help would be appreciated.

Regards,
Jon

--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________

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

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

Re: changing labeling of axes

Slavin, Jonathan
I was able to get the major ticks labeled in the way I want by doing:
ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x,pos: r'$10^{%i}$'% x))
I don't get any minor ticks that way.  I could live with that, though I'd prefer to get logarithmically spaced minor ticks.  I tried 
ax.xaxis.set_minor_locator(ticker.LogLocator())
but that seemed to have no effect.

The idea of providing logarithmically  bin edges seems like a good one, but as Tom says does not do what is expected.  I tried using numpy's histogram2d providing logarithmically spaced bins and it worked as expected (though for some reason the array returned was transposed), so I think this is a bug in hist2d.

Jon

On Wed, Nov 8, 2017 at 12:01 PM, Thomas Caswell <[hidden email]> wrote:
You can do this with the formatter on the axis, see https://matplotlib.org/gallery/ticks_and_spines/tick-formatters.html#sphx-glr-gallery-ticks-and-spines-tick-formatters-py and https://matplotlib.org/api/ticker_api.html

You can also pass log-spaced bins to `hist2D` and then set the axis scales to log (but trying to construct an example did not got as I expected...)

Tom

On Wed, Nov 8, 2017 at 10:10 AM Slavin, Jonathan <[hidden email]> wrote:
Hi,

I'm wondering if anyone knows of a good way to relabel axes for a particular case I'm dealing with. I'm making a 2D histogram, using hist2d of course.  The thing is that for my case, where I'm plotting the density vs. pressure for results from a hydro run, it makes more sense to use the log of the quantities as the inputs for hist2d.  So then I need to give the log of the density and pressure to hist2d. This works fine for making the histogram, but then the axes are labelled with log10(n), log10(P).  I'd prefer to have the axes labelled as for a logarithmic plot, so instead of say 4.0, 10^4.  Is there a simple way of doing that. I'm pretty sure I can just specify by hand the ticks and tick labels, though that's kind of a pain. Any help would be appreciated.

Regards,
Jon

--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________

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



--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: (617) 496-7981       Cambridge, MA 02138-1516
cell: (781) 363-0035             USA
________________________________________________________


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

Re: changing labeling of axes

Benjamin Root
By default, the minor ticks are turned off. You have to turn it on yourself. This is one of the things that set_xscale('log') does for you automatically.

On Wed, Nov 8, 2017 at 1:50 PM, Slavin, Jonathan <[hidden email]> wrote:
I was able to get the major ticks labeled in the way I want by doing:
ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x,pos: r'$10^{%i}$'% x))
I don't get any minor ticks that way.  I could live with that, though I'd prefer to get logarithmically spaced minor ticks.  I tried 
ax.xaxis.set_minor_locator(ticker.LogLocator())
but that seemed to have no effect.

The idea of providing logarithmically  bin edges seems like a good one, but as Tom says does not do what is expected.  I tried using numpy's histogram2d providing logarithmically spaced bins and it worked as expected (though for some reason the array returned was transposed), so I think this is a bug in hist2d.

Jon

On Wed, Nov 8, 2017 at 12:01 PM, Thomas Caswell <[hidden email]> wrote:
You can do this with the formatter on the axis, see https://matplotlib.org/gallery/ticks_and_spines/tick-formatters.html#sphx-glr-gallery-ticks-and-spines-tick-formatters-py and https://matplotlib.org/api/ticker_api.html

You can also pass log-spaced bins to `hist2D` and then set the axis scales to log (but trying to construct an example did not got as I expected...)

Tom

On Wed, Nov 8, 2017 at 10:10 AM Slavin, Jonathan <[hidden email]> wrote:
Hi,

I'm wondering if anyone knows of a good way to relabel axes for a particular case I'm dealing with. I'm making a 2D histogram, using hist2d of course.  The thing is that for my case, where I'm plotting the density vs. pressure for results from a hydro run, it makes more sense to use the log of the quantities as the inputs for hist2d.  So then I need to give the log of the density and pressure to hist2d. This works fine for making the histogram, but then the axes are labelled with log10(n), log10(P).  I'd prefer to have the axes labelled as for a logarithmic plot, so instead of say 4.0, 10^4.  Is there a simple way of doing that. I'm pretty sure I can just specify by hand the ticks and tick labels, though that's kind of a pain. Any help would be appreciated.

Regards,
Jon

--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________

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



--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________


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



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

Re: changing labeling of axes

Slavin, Jonathan
Actually I have now found a better approach. What I do is create the histogram using numpy.histogram2d with logarithmic bins as input.  Then I use pcolormesh to create the plot.  I'm basically following the lead shown on the numpy.histogram2d page.

Jon

On Wed, Nov 8, 2017 at 2:01 PM, Benjamin Root <[hidden email]> wrote:
By default, the minor ticks are turned off. You have to turn it on yourself. This is one of the things that set_xscale('log') does for you automatically.

On Wed, Nov 8, 2017 at 1:50 PM, Slavin, Jonathan <[hidden email]> wrote:
I was able to get the major ticks labeled in the way I want by doing:
ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x,pos: r'$10^{%i}$'% x))
I don't get any minor ticks that way.  I could live with that, though I'd prefer to get logarithmically spaced minor ticks.  I tried 
ax.xaxis.set_minor_locator(ticker.LogLocator())
but that seemed to have no effect.

The idea of providing logarithmically  bin edges seems like a good one, but as Tom says does not do what is expected.  I tried using numpy's histogram2d providing logarithmically spaced bins and it worked as expected (though for some reason the array returned was transposed), so I think this is a bug in hist2d.

Jon

On Wed, Nov 8, 2017 at 12:01 PM, Thomas Caswell <[hidden email]> wrote:
You can do this with the formatter on the axis, see https://matplotlib.org/gallery/ticks_and_spines/tick-formatters.html#sphx-glr-gallery-ticks-and-spines-tick-formatters-py and https://matplotlib.org/api/ticker_api.html

You can also pass log-spaced bins to `hist2D` and then set the axis scales to log (but trying to construct an example did not got as I expected...)

Tom

On Wed, Nov 8, 2017 at 10:10 AM Slavin, Jonathan <[hidden email]> wrote:
Hi,

I'm wondering if anyone knows of a good way to relabel axes for a particular case I'm dealing with. I'm making a 2D histogram, using hist2d of course.  The thing is that for my case, where I'm plotting the density vs. pressure for results from a hydro run, it makes more sense to use the log of the quantities as the inputs for hist2d.  So then I need to give the log of the density and pressure to hist2d. This works fine for making the histogram, but then the axes are labelled with log10(n), log10(P).  I'd prefer to have the axes labelled as for a logarithmic plot, so instead of say 4.0, 10^4.  Is there a simple way of doing that. I'm pretty sure I can just specify by hand the ticks and tick labels, though that's kind of a pain. Any help would be appreciated.

Regards,
Jon

--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________

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



--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: <a href="tel:(617)%20496-7981" value="+16174967981" target="_blank">(617) 496-7981       Cambridge, MA 02138-1516
cell: <a href="tel:(781)%20363-0035" value="+17813630035" target="_blank">(781) 363-0035             USA
________________________________________________________


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





--
________________________________________________________
Jonathan D. Slavin                 Harvard-Smithsonian CfA
[hidden email]       60 Garden Street, MS 83
phone: (617) 496-7981       Cambridge, MA 02138-1516
cell: (781) 363-0035             USA
________________________________________________________


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