Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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
c    map.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)
like image 221
andreas-h Avatar asked Feb 09 '12 16:02

andreas-h


1 Answers

The docs for pcolormesh say:

Masked array support is implemented via cmap and norm; in contrast, pcolor() simply does not draw quadrilaterals with masked colors or vertices.

So use pcolormesh instead:

import matplotlib.pyplot as plt
import numpy as np

D = np.random.randn(12*72).reshape((12, 72))
D[4, :] = np.nan
D[6, 6] = np.nan

D = np.ma.masked_invalid(D)

cmap = plt.get_cmap('bwr')
cmap.set_bad(color = 'k', alpha = 1.)

xbin = np.linspace(0, 12, 13)
ybin = np.linspace(-90, 90, 73)

fig = plt.figure()
ax = fig.add_subplot(111)
pl = ax.pcolormesh(xbin, ybin, D.T, cmap = cmap, edgecolors = 'None',
                vmin = -5, vmax = 5)
plt.show()

enter image description here

like image 61
unutbu Avatar answered Sep 29 '22 02:09

unutbu