Color of masked values in pcolor plots

Hi,

I m trying to plot some data with pcolor. The data should be plotted on a ring. Inside and outside of the ring should be white area. However, now I have black area. Where is my mistake? Minimal working example:

from matplotlib import cm
import matplotlib.pyplot as plt
from pylab import *

import numpy as np
import scipy as sp

X = np.arange(-1.0, 1.01, 0.01)
Y = np.arange(-1.0, 1.01, 0.01)
X, Y = np.meshgrid(X, Y)

[r, R] = [0.25, 1.0]
an = sp.linspace(0,2*sp.pi,100)

fig = plt.figure(1, figsize=(6,6))

ax = fig.add_subplot(111, aspect = 'equal')
Z = X**2 + Y**2
Z[(X**2+Y**2 < r**2) | (X**2+Y**2 > R**2)] = np.ma.masked
cm.hot.set_bad('white', alpha=None)
plot = pcolor(X, Y, Z, cmap=cm.hot)
plt.plot(r*sp.cos(an), r*sp.sin(an), 'k')
plt.plot(R*sp.cos(an), R*sp.sin(an), 'k')
axis([-1.0, 1.0, -1.0, 1.0])

plt.show()

see http://matplotlib.1069221.n5.nabble.com/cmap-set-bad-not-showing-any-effect-with-pcolor-td27530.html

Hi,

I m trying to plot some data with pcolor. The data should be plotted on a ring. Inside and outside of the ring should be white area. However, now I have black area. Where is my mistake? Minimal working example:

from matplotlib import cm
import matplotlib.pyplot as plt
from pylab import *

import numpy as np
import scipy as sp
   
X = np.arange(-1.0, 1.01, 0.01)
Y = np.arange(-1.0, 1.01, 0.01)
X, Y = np.meshgrid(X, Y)

[r, R] = [0.25, 1.0]
an = sp.linspace(0,2*sp.pi,100)

fig = plt.figure(1, figsize=(6,6))

ax = fig.add_subplot(111, aspect = 'equal')
Z = X**2 + Y**2
Z[(X**2+Y**2 < r**2) | (X**2+Y**2 > R**2)] = np.ma.masked
cm.hot.set_bad('white', alpha=None)
plot = pcolor(X, Y, Z, cmap=cm.hot)

I think part of the problem is that Z is not a masked_array. This works for me. I set_bad to green to show the effect.

Z = np.ma.masked_array(X**2 + Y**2,
                       mask = (X**2+Y**2 <= r**2) | (X**2+Y**2 > R**2))
cm.hot.set_bad('green', alpha=None)
plot = pcolormesh(X, Y, Z, cmap=cm.hot)

···

On Dec 1, 2013, at 10:11 AM, lin.huan@...4471... wrote:

plt.plot(r*sp.cos(an), r*sp.sin(an), 'k')
plt.plot(R*sp.cos(an), R*sp.sin(an), 'k')
axis([-1.0, 1.0, -1.0, 1.0])

plt.show()

The problem is that Z is not a masked array. Change the first line above to Z = np.ma.array(X**2 + Y**2).

Also, use pcolormesh instead of pcolor. Pcolormesh is much faster.

Eric

···

On 2013/12/01 5:11 AM, lin.huan@...4471... wrote:

Z = X**2 + Y**2
Z[(X**2+Y**2 < r**2) | (X**2+Y**2 > R**2)] = np.ma.masked