cmap.set_bad() not showing any effect with pcolor()

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 :slight_smile:

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

路路路

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 :slight_smile:

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)

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

BTW: pcolormesh will do what you want.

-Jeff

路路路

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 :slight_smile:

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 : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

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鈥檛 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

路路路

On Fri, Feb 10, 2012 at 11:23 AM, Andreas H. <lists@鈥3957鈥7鈥> 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鈥檚 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

路路路

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.

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

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

路路路

On 02/10/2012 07:23 AM, Andreas H. wrote:

Cheers,
Andreas.