|
From: Benjamin R. <ben...@ou...> - 2013-03-06 14:38:49
|
On Tue, Mar 5, 2013 at 5:33 PM, Mahe <mah...@gm...> wrote: > > Benjamin Root <ben.root@...> writes: > > > > > On Tue, Feb 1, 2011 at 11:09 AM, Francesco Benincasa > <francesco.benincasa- > DuY...@pu...> 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@... > > 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 > > 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 |