|
From: Andreas H. <li...@hi...> - 2012-02-09 16:22:24
|
Hi,
I'm trying to use pcolor on a masked array. I would like masked elements
to show up in a special color. I have written some code, but it does not
seem to work:
I would appreciate any help :)
Cheers,
Andreas.
---8<-------
import matplotlib as mpl
import matplotlib.pyplot as plt
from numpy import linspace
from numpy.random import randn
from numpy.ma import masked_invalid
D = randn(12*72).reshape((12,72))
D[4,:] = nan
D[6,6] = nan
D = masked_invalid(D)
cmap = mpl.cm.bwr
cmap.set_bad('k', 1.)
xbin = linspace(0, 12, 13)
ybin = linspace(-90, 90, 73)
fig = plt.figure()
spl = fig.add_subplot(111)
pl = spl.pcolor(xbin, ybin, D.T, cmap=cmap, edgecolors='none',
vmin=-5, vmax=5)
|
|
From: Jeff W. <jef...@no...> - 2012-02-09 17:49:18
|
On 2/9/12 9:05 AM, Andreas H. wrote:
> Hi,
>
> I'm trying to use pcolor on a masked array. I would like masked elements
> to show up in a special color. I have written some code, but it does not
> seem to work:
>
> I would appreciate any help :)
>
> Cheers,
> Andreas.
>
> ---8<-------
>
> import matplotlib as mpl
> import matplotlib.pyplot as plt
>
> from numpy import linspace
> from numpy.random import randn
> from numpy.ma import masked_invalid
>
> D = randn(12*72).reshape((12,72))
> D[4,:] = nan
> D[6,6] = nan
>
> D = masked_invalid(D)
>
> cmap = mpl.cm.bwr
> cmap.set_bad('k', 1.)
>
> xbin = linspace(0, 12, 13)
> ybin = linspace(-90, 90, 73)
>
> fig = plt.figure()
> spl = fig.add_subplot(111)
> pl = spl.pcolor(xbin, ybin, D.T, cmap=cmap, edgecolors='none',
> vmin=-5, vmax=5)
Andreas: That's because pcolor only fills polygons that are not masked
- it does do anything with the masked ones.
From the docstring:
*X*, *Y* and *C* may be masked arrays. If either C[i, j], or one
of the vertices surrounding C[i,j] (*X* or *Y* at [i, j], [i+1, j],
[i, j+1],[i+1, j+1]) is masked, *]nothing is plotted.
I suppose pcolor could be modified to fill the masked polygons with the
color indicated by cmap.set_bad - I think that's what most people would
expect.
-Jeff
--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jef...@no...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg
|
|
From: Jeff W. <jef...@no...> - 2012-02-09 23:46:24
|
On 2/9/12 10:49 AM, Jeff Whitaker wrote:
> On 2/9/12 9:05 AM, Andreas H. wrote:
>> Hi,
>>
>> I'm trying to use pcolor on a masked array. I would like masked elements
>> to show up in a special color. I have written some code, but it does not
>> seem to work:
>>
>> I would appreciate any help :)
>>
>> Cheers,
>> Andreas.
>>
>> ---8<-------
>>
>> import matplotlib as mpl
>> import matplotlib.pyplot as plt
>>
>> from numpy import linspace
>> from numpy.random import randn
>> from numpy.ma import masked_invalid
>>
>> D = randn(12*72).reshape((12,72))
>> D[4,:] = nan
>> D[6,6] = nan
>>
>> D = masked_invalid(D)
>>
>> cmap = mpl.cm.bwr
>> cmap.set_bad('k', 1.)
>>
>> xbin = linspace(0, 12, 13)
>> ybin = linspace(-90, 90, 73)
>>
>> fig = plt.figure()
>> spl = fig.add_subplot(111)
>> pl = spl.pcolor(xbin, ybin, D.T, cmap=cmap, edgecolors='none',
>> vmin=-5, vmax=5)
> Andreas: That's because pcolor only fills polygons that are not masked
> - it does do anything with the masked ones.
>
> From the docstring:
>
> *X*, *Y* and *C* may be masked arrays. If either C[i, j], or one
> of the vertices surrounding C[i,j] (*X* or *Y* at [i, j], [i+1, j],
> [i, j+1],[i+1, j+1]) is masked, *]nothing is plotted.
>
> I suppose pcolor could be modified to fill the masked polygons with the
> color indicated by cmap.set_bad - I think that's what most people would
> expect.
>
> -Jeff
>
>
BTW: pcolormesh will do what you want.
-Jeff
--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jef...@no...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg
|
|
From: Andreas H. <li...@hi...> - 2012-02-10 17:23:25
|
Thanks Jeff! I really should have looked at the docs more carefully. >> I suppose pcolor could be modified to fill the masked polygons with the >> color indicated by cmap.set_bad - I think that's what most people would >> expect. Yes, I definitely second this. It would produce more expected results. Cheers, Andreas. |
|
From: Benjamin R. <ben...@ou...> - 2012-02-10 17:48:32
|
On Fri, Feb 10, 2012 at 11:23 AM, Andreas H. <li...@hi...> wrote: > Thanks Jeff! > > I really should have looked at the docs more carefully. > > >> I suppose pcolor could be modified to fill the masked polygons with the > >> color indicated by cmap.set_bad - I think that's what most people would > >> expect. > > Yes, I definitely second this. It would produce more expected results. > > Cheers, > Andreas. > > IIRC, the reason for the difference between pcolor and pcolormesh is that pcolor allows for arbitrary, non-regular domains while pcolormesh assumes some sort of regularity. As part of the process for creating the pcolor, the masks for all inputs are &-ed together and the inputs are truncated accordingly. The reason why nothing is drawn for those polygons is that the core part of pcolor never sees them and doesn't know they even exist. In order to achieve the requested behavior, we would need to &-together only the domain inputs and handle the masked Z values specially (particularly issues surrounding how to handle drawing edges of these polygons). Such a change could also break code that dealt with the set_array() (set_data()?) method of the pcolor object (this is very common for animations). Any change here needs to be carefully considered. Ben Root |
|
From: Jeff W. <jef...@no...> - 2012-02-10 18:04:43
|
On 2/10/12 10:23 AM, Andreas H. wrote: > Thanks Jeff! > > I really should have looked at the docs more carefully. > >>> I suppose pcolor could be modified to fill the masked polygons with the >>> color indicated by cmap.set_bad - I think that's what most people would >>> expect. > Yes, I definitely second this. It would produce more expected results. > > Cheers, > Andreas. > > Andreas: I created a patch for this and submitted a pull request (#701). -Jeff -- Jeffrey S. Whitaker Phone : (303)497-6313 Meteorologist FAX : (303)497-6449 NOAA/OAR/PSD R/PSD1 Email : Jef...@no... 325 Broadway Office : Skaggs Research Cntr 1D-113 Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg |
|
From: Eric F. <ef...@ha...> - 2012-02-10 18:29:29
|
On 02/10/2012 07:23 AM, Andreas H. wrote: > Thanks Jeff! > > I really should have looked at the docs more carefully. > >>> I suppose pcolor could be modified to fill the masked polygons with the >>> color indicated by cmap.set_bad - I think that's what most people would >>> expect. > > Yes, I definitely second this. It would produce more expected results. I agree. I think the reason it doesn't at present is that support for bad values and out-of-range values in color maps was added after the masked-array support was added to pcolor. Ben is correct, though, that this change in long-standing behavior could break existing user code. Eric > > Cheers, > Andreas. |