Setting transparent pixels during overplotting

Hi,

I would like to draw a contour plot on top of an image and such that any values of the countour plot < x are made transparent.

Here is what I am doing at the moment to handle the overplotting:

from matplotlib import pyplot as plt

fig = plt.figure()

axes = fig.add_subplot(111)

contour = plt.imshow(contour_data, extent=contour_extent,cmap=contour_cmap, origin=‘lower’, zorder=10)

im = plt.imshow(im_data, extent=im_extent, cmap=im_cmap, origin=‘lower’, zorder=1)

plt.show()

Any suggestions? One thing I thought about doing is converting im_data and contour_data from grayscale to RGBA, and setting the alpha channel to 0 for all data with value less than x, but I was hoping there might be a more straight-forward way to handle this.

Thanks,
Keith

Just use a numpy masked array and it will do exactly what you want.

contour_data = np.ma.masked_array(contour_data, mask=(contour_data < 0))

Cheers!
Ben Root

···

On Friday, November 25, 2011, Keith Hughitt <keith.hughitt@…287…> wrote:

Hi,
I would like to draw a contour plot on top of an image and such that any values of the countour plot < x are made transparent.

Here is what I am doing at the moment to handle the overplotting:

from matplotlib import pyplot as plt
fig = plt.figure()
axes = fig.add_subplot(111)
contour = plt.imshow(contour_data, extent=contour_extent,cmap=contour_cmap, origin=‘lower’, zorder=10)

im = plt.imshow(im_data, extent=im_extent, cmap=im_cmap, origin=‘lower’, zorder=1)
plt.show()

Any suggestions? One thing I thought about doing is converting im_data and contour_data from grayscale to RGBA, and setting the alpha channel to 0 for all data with value less than x, but I was hoping there might be a more straight-forward way to handle this.

Thanks,
Keith