color missing value with contourf

Benjamin Root <ben.root@...83...> writes:

Hi all,
I'm using pygrads for plotting maps from netcdf files.
I use the contourf method, but I'm not able to fill the region where there are
no value (there is the missing value -999) with a color. It seems to ignore
the set_bad method that I used to make the colormap.
Any suggestions?
Thank you very much in advance.
--
> Francesco Benincasa

Most likely, the issue is that set_bad is more for setting the color when

encountering masked values (through masked arrays). As a quick and dirty way to
deal with it, try setting that color through the set_under() method.The correct
way to do this is to use set_bad, but convert your numpy array that you are
displaying into a masked array like so:z_ma = np.ma.masked_array(z, mask=(z ==
-999))and use contourf on z_ma.Let us know how that works for you.Ben Root

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires
February 28th, so secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsight-sfd2d

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@...83...
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi !
I have had the same issue (set_bad not taking effect with nans), and transformed
the data into a masked array. But it does not seem to work...
Here a minimal example:

import matplotlib.pyplot as plt
import numpy as np

plt.clf()
x = np.linspace(-180,180,100)
y = np.linspace(-90,90,100)
x, y = np.meshgrid(x,y)
data = np.cos(x/180*np.pi) + np.sin(y/180*np.pi)
data[(y<50)&(y>30)&(x<50)&(x>30)] = np.nan
data = np.ma.masked_array(data, mask = np.isnan(data)) # has no effect
ncol = 20
cbar = [-1,1]
palette = plt.cm.Blues
palette.set_bad('green')
palette.set_over('red')
palette.set_under('black')
cs = plt.contourf(x,y,data,np.linspace(cbar[0],cbar[1],ncol), cmap=palette,
extend='both')
plt.colorbar()
cs.set_clim(cbar) # need that for set_upper and set_lower to take effect
plt.show()

There is already that small bug where one needs to call set_clim for set_upper
and set_lower, maybe something similar is needed for set_bad?
Any idea?

Many thanks,
Mahe

···

On Tue, Feb 1, 2011 at 11:09 AM, Francesco Benincasa <francesco.benincasa- DuYNTNMygGQ@...1455...> wrote:

Your problem is very, very subtle, and we probably should handle this better. The issue is, I think, that because of the way contourf works, the colormap is applied to the list of polygon (or patch) collections, each having a value for its level. Because there wouldn’t be a “nan” level, there is no polygon or patch at all for that spot. Indeed, if you change the background color of the plot, the white patch becomes whatever color the background is.

I hope this clears it up for you.

Ben Root

···

On Tue, Mar 5, 2013 at 5:33 PM, Mahe <mahe.perrette@…287…> wrote:

Benjamin Root <ben.root@…83…> writes:

On Tue, Feb 1, 2011 at 11:09 AM, Francesco Benincasa <francesco.benincasa- > > DuYNTNMygGQ@…1455…> wrote:

Hi all,

I’m using pygrads for plotting maps from netcdf files.

I use the contourf method, but I’m not able to fill the region where there are

no value (there is the missing value -999) with a color. It seems to ignore

the set_bad method that I used to make the colormap.

Any suggestions?

Thank you very much in advance.

Francesco Benincasa

Most likely, the issue is that set_bad is more for setting the color when

encountering masked values (through masked arrays). As a quick and dirty way to

deal with it, try setting that color through the set_under() method.The correct
way to do this is to use set_bad, but convert your numpy array that you are

displaying into a masked array like so:z_ma = np.ma.masked_array(z, mask=(z ==

-999))and use contourf on z_ma.Let us know how that works for you.Ben Root


Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!

Finally, a world-class log management solution at an even better price-free!

Download using promo code Free_Logger_4_Dev2Dev. Offer expires

February 28th, so secure your free ArcSight Logger TODAY!

http://p.sf.net/sfu/arcsight-sfd2d


Matplotlib-users mailing list

Matplotlib-users@…83…

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hi !

I have had the same issue (set_bad not taking effect with nans), and transformed

the data into a masked array. But it does not seem to work…

Here a minimal example:

import matplotlib.pyplot as plt

import numpy as np

plt.clf()

x = np.linspace(-180,180,100)

y = np.linspace(-90,90,100)

x, y = np.meshgrid(x,y)

data = np.cos(x/180np.pi) + np.sin(y/180np.pi)

data[(y<50)&(y>30)&(x<50)&(x>30)] = np.nan

data = np.ma.masked_array(data, mask = np.isnan(data)) # has no effect

ncol = 20

cbar = [-1,1]

palette = plt.cm.Blues

palette.set_bad(‘green’)

palette.set_over(‘red’)

palette.set_under(‘black’)

cs = plt.contourf(x,y,data,np.linspace(cbar[0],cbar[1],ncol), cmap=palette,

extend=‘both’)

plt.colorbar()

cs.set_clim(cbar) # need that for set_upper and set_lower to take effect

plt.show()

There is already that small bug where one needs to call set_clim for set_upper

and set_lower, maybe something similar is needed for set_bad?

Any idea?

Many thanks,

Mahe