Manually limiting value ranges

Hi,
I've been trying to do this for a while but just can't get it to
work. :frowning:

There are 2 things I want to do.

1. I want to limit the value range in a line plot from
matplotlib.pyplot.plot(). I thought that clip_box would do exactly
that but setting something like [[-1,1],[-5,5]] or the like doesn't
seem to have any effect.

2. In a 2D map plot from matplotlib.pyplot.imshow(), I would like to
set the limits for the colour bar manually. I.e. I want the colours to
be equally distributed over a fixed range.

In both cases, the background to my attempt of using fixed values is
that I'm producing a time series of plots via script and want all the
plots to be directly comparable.

Did I overlook this in the examples (or the documentation) or is there
really no simple way of doing this?

Cheers,
Christian

Christian,
The answer to your second question is a little more involved and I think there are a few posts regarding custom colormaps on the mailing list that may be of interest…I’d try searching through those. I may not be the best person to answer that question.
Also you may be interested in exploring the kwargs vmin and vmax of the imshow command. They may do what you need as well:
http://matplotlib.sourceforge.net/api/axes_api.html?highlight=imshow#matplotlib.axes.Axes.imshow
As for your first question try the following in ipython with the -pylab flag:
import numpy as N
a = N.random.rand(50)
a
=100
plot(a)
ax = gca()
ax.set_xlim(0,25) # The set_xlim and set_ylim may be what you’re looking for…
ax.set_ylim(0,50)
show()

···

— On ** Mon, 12/29/08, Christian Lerrahn <lists@…1766…>** wrote:

From: Christian Lerrahn <lists@…1766…>
Subject: [Matplotlib-users] Manually limiting value ranges
To: matplotlib-users@lists.sourceforge.net
Date: Monday, December 29, 2008, 11:19 PM

Hi,
I've been trying to do this for a while but just can't get it to
work. :(

There are 2 things I want to do.

1. I want to limit the value range in a line plot from
matplotlib.pyplot.plot(). I thought that clip_box would do exactly
that but setting something like [[-1,1],[-5,5]] or the like doesn't
seem to have any effect.

2. In a 2D map plot from matplotlib.pyplot.imshow(), I would like to
set the limits for the colour bar manually. I.e. I want the colours to
be equally distributed over a fixed range.

 In both cases, the background to my attempt of
using fixed values is
that I'm producing a time series of plots via script and want all the
plots to be directly comparable.

Did I overlook this in the examples (or the documentation) or is there
really no simple way of doing this?

Cheers,
Christian

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi,
the code given works for me in principle. If I run ipython without the
-pylab flag and do an

import * from pylab

at the start instead, I can run this code and it runs fine. (If not,
the plot() will imply the show and everything after that doesn't matter
any more.)

However, I'm now running into a new problem. I'm doing lots of separate
plots like that (hence the need for a fixed scale). To that end I run
the code in a loop which looks like

for t in range(maxt):
    
    fig = p.figure(1)
    fig.clf()
    ax = fig.add_subplot(111)
    p.plot(pvar[t,ipos,:])
    p.plot(pvar[t,:,ipos])

    ax = gca()
    ax.set_ylim(-10,10) #
   
    filename = "%s-sliced%04d%s.png" % (var,t,dim)
    ptime = "%0.3f" % (timevar[t]/3600)
    p.title(var + ' at t=' + ptime + 'h')
    p.savefig(filename)
    p.clf()

Now, this will result in the error

  File "./swimplot-slice.py", line 63, in <module>
    ax.set_ylim(-10,10)
  File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line 2044, in set_ylim
    ymin = self.convert_yunits(ymin)
  File "/usr/lib/python2.5/site-packages/matplotlib/artist.py", line 111, in convert_yunits
    return ax.yaxis.convert_units(y)
  File "/usr/lib/python2.5/site-packages/matplotlib/axis.py", line 979, in convert_units
    self.converter = munits.registry.get_converter(x)
  File "//usr/lib/python2.5/site-packages/matplotlib/units.py", line 137, in get_converter
    converter = self.get_converter( thisx )
  File "//usr/lib/python2.5/site-packages/matplotlib/units.py", line
  137, in get_converter

With the last line being repeated for every iteration (so I assume). Then after
plenty of error messages like the last one above, I get

  File "//usr/lib/python2.5/site-packages/matplotlib/units.py", line 130, in get_converter
    if converter is None and iterable(x):
RuntimeError: maximum recursion depth exceeded

Where does this recursion come from? Do I have to clear something in each iteration
to avoid that? If so, how do I do that?

Cheers,
Christian

···

On Tue, 30 Dec 2008 07:31:57 -0800 (PST) B Clowers <clowersb@...9...> wrote:

Christian,

The answer to your second question is a little more involved and I
think there are a few posts regarding custom colormaps on the mailing
list that may be of interest...I'd try searching through those. I
may not be the best person to answer that question.

Also you may be interested in exploring the *kwargs vmin and vmax of
the imshow command. They may do what you need as well:

http://matplotlib.sourceforge.net/api/axes_api.html?highlight=imshow#matplotlib.axes.Axes.imshow

As for your first question try the following in ipython with the
-pylab flag:

import numpy as N
a = N.random.rand(50)
a*=100
plot(a)
ax = gca()
ax.set_xlim(0,25) # The set_xlim and set_ylim may be what you're
looking for... ax.set_ylim(0,50)
show()

--- On Mon, 12/29/08, Christian Lerrahn <lists@...1766...> wrote:
From: Christian Lerrahn <lists@...1766...>
Subject: [Matplotlib-users] Manually limiting value ranges
To: matplotlib-users@lists.sourceforge.net
Date: Monday, December 29, 2008, 11:19 PM

Hi,
I've been trying to do this for a while but just can't get it to
work. :frowning:

There are 2 things I want to do.

1. I want to limit the value range in a line plot from
matplotlib.pyplot.plot(). I thought that clip_box would do exactly
that but setting something like [[-1,1],[-5,5]] or the like doesn't
seem to have any effect.

2. In a 2D map plot from matplotlib.pyplot.imshow(), I would like to
set the limits for the colour bar manually. I.e. I want the colours to
be equally distributed over a fixed range.

In both cases, the background to my attempt of using fixed values is
that I'm producing a time series of plots via script and want all the
plots to be directly comparable.

Did I overlook this in the examples (or the documentation) or is there
really no simple way of doing this?

Cheers,
Christian

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

Hi,
I've figured this out. There was actually invalid input for the range
the way my code really was. The code I had posted was completely
wrong, anyway. All good now. :slight_smile:

The code

p.plot(pvar[t,ipos,:])
ax = gca()
ax.set_ylim(-10,10)
p.show()

works with or without a loop wrapped around it. :slight_smile:

Cheers,
Christian

···

On Wed, 7 Jan 2009 10:21:30 +1100 Christian Lerrahn <lists@...1766...> wrote:

Hi,
the code given works for me in principle. If I run ipython without the
-pylab flag and do an

import * from pylab

at the start instead, I can run this code and it runs fine. (If not,
the plot() will imply the show and everything after that doesn't
matter any more.)

However, I'm now running into a new problem. I'm doing lots of
separate plots like that (hence the need for a fixed scale). To that
end I run the code in a loop which looks like

for t in range(maxt):
    
    fig = p.figure(1)
    fig.clf()
    ax = fig.add_subplot(111)
    p.plot(pvar[t,ipos,:])
    p.plot(pvar[t,:,ipos])

    ax = gca()
    ax.set_ylim(-10,10) #
   
    filename = "%s-sliced%04d%s.png" % (var,t,dim)
    ptime = "%0.3f" % (timevar[t]/3600)
    p.title(var + ' at t=' + ptime + 'h')
    p.savefig(filename)
    p.clf()

Now, this will result in the error

  File "./swimplot-slice.py", line 63, in <module>
    ax.set_ylim(-10,10)
  File "/usr/lib/python2.5/site-packages/matplotlib/axes.py", line
2044, in set_ylim ymin = self.convert_yunits(ymin)
  File "/usr/lib/python2.5/site-packages/matplotlib/artist.py", line
111, in convert_yunits return ax.yaxis.convert_units(y)
  File "/usr/lib/python2.5/site-packages/matplotlib/axis.py", line
979, in convert_units self.converter =
munits.registry.get_converter(x) File
"//usr/lib/python2.5/site-packages/matplotlib/units.py", line 137, in
get_converter converter = self.get_converter( thisx ) File
"//usr/lib/python2.5/site-packages/matplotlib/units.py", line 137, in
get_converter

With the last line being repeated for every iteration (so I assume).
Then after plenty of error messages like the last one above, I get

  File "//usr/lib/python2.5/site-packages/matplotlib/units.py", line
130, in get_converter if converter is None and iterable(x):
RuntimeError: maximum recursion depth exceeded

Where does this recursion come from? Do I have to clear something in
each iteration to avoid that? If so, how do I do that?

Cheers,
Christian

On Tue, 30 Dec 2008 07:31:57 -0800 (PST) > B Clowers <clowersb@...9...> wrote:

> Christian,
>
> The answer to your second question is a little more involved and I
> think there are a few posts regarding custom colormaps on the
> mailing list that may be of interest...I'd try searching through
> those. I may not be the best person to answer that question.
>
> Also you may be interested in exploring the *kwargs vmin and vmax of
> the imshow command. They may do what you need as well:
>
> http://matplotlib.sourceforge.net/api/axes_api.html?highlight=imshow#matplotlib.axes.Axes.imshow
>
> As for your first question try the following in ipython with the
> -pylab flag:
>
> import numpy as N
> a = N.random.rand(50)
> a*=100
> plot(a)
> ax = gca()
> ax.set_xlim(0,25) # The set_xlim and set_ylim may be what you're
> looking for... ax.set_ylim(0,50)
> show()
>
> --- On Mon, 12/29/08, Christian Lerrahn <lists@...1766...> wrote:
> From: Christian Lerrahn <lists@...1766...>
> Subject: [Matplotlib-users] Manually limiting value ranges
> To: matplotlib-users@lists.sourceforge.net
> Date: Monday, December 29, 2008, 11:19 PM
>
> Hi,
> I've been trying to do this for a while but just can't get it to
> work. :frowning:
>
> There are 2 things I want to do.
>
> 1. I want to limit the value range in a line plot from
> matplotlib.pyplot.plot(). I thought that clip_box would do exactly
> that but setting something like [[-1,1],[-5,5]] or the like doesn't
> seem to have any effect.
>
> 2. In a 2D map plot from matplotlib.pyplot.imshow(), I would like to
> set the limits for the colour bar manually. I.e. I want the colours
> to be equally distributed over a fixed range.
>
> In both cases, the background to my attempt of using fixed values is
> that I'm producing a time series of plots via script and want all
> the plots to be directly comparable.
>
> Did I overlook this in the examples (or the documentation) or is
> there really no simple way of doing this?
>
> Cheers,
> Christian
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> matplotlib-users List Signup and Options
>
>
>
>

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options