Discussion:
Easy way to map nans as grey with pcolorm?
(too old to reply)
Erica B
2012-05-29 00:01:05 UTC
Permalink
Hey All,

Apologies if this is a duplicate post, but I couldn't find an answer among existing threads.

Does anyone know how to get pcolorm to plot NaNs as grey without grey showing on the colorbar?

I'm plotting satellite data using the mapping toolbox and pcolorm. There are missing values (NaNs) in the data. What I want is for NaNs to plot as grey on the map, with only the color range of the actual data showing on the colorbar and without tacking on a grey cell at the end of the colormap. What I'm getting is NaNs being plotted as the lowest color on the colorbar.

I've investigated just patching over the map with NaNs set to grey, re-interpolating my data to the extent of the colormap/colorbar I wish to show, setting 'alphadata' etc, but so far none of these has worked the way I want it to. I also don't want to just set the NaNs to zero, as I'm plotting a percent difference map with 0% set to white, and I need to differentiate between areas of no data, and areas of no change.

Any help would be much appreciated! At this point, I've exhausted both google and my own brain.

Thanks,

EB
Maciej
2012-05-29 00:35:06 UTC
Permalink
Post by Erica B
Hey All,
Apologies if this is a duplicate post, but I couldn't find an answer among existing threads.
Does anyone know how to get pcolorm to plot NaNs as grey without grey showing on the colorbar?
I'm plotting satellite data using the mapping toolbox and pcolorm. There are missing values (NaNs) in the data. What I want is for NaNs to plot as grey on the map, with only the color range of the actual data showing on the colorbar and without tacking on a grey cell at the end of the colormap. What I'm getting is NaNs being plotted as the lowest color on the colorbar.
I've investigated just patching over the map with NaNs set to grey, re-interpolating my data to the extent of the colormap/colorbar I wish to show, setting 'alphadata' etc, but so far none of these has worked the way I want it to. I also don't want to just set the NaNs to zero, as I'm plotting a percent difference map with 0% set to white, and I need to differentiate between areas of no data, and areas of no change.
Any help would be much appreciated! At this point, I've exhausted both google and my own brain.
Thanks,
EB
Hi Erica,

I had similar problem to your. I was using imagesc function to plot some picture based on matrix data which included NaNs. In my case the NaN pixels exchanged to white rectangles just via ractangle function. This is maybe not perfect method (it takes about 10 second for 120x100 data matrix), but it works:). Please consider this approach and after that I will provide you further help.

Regards
Maciek
Bruno Luong
2012-05-29 03:01:07 UTC
Permalink
This might fit your need:

http://www.mathworks.com/matlabcentral/fileexchange/20516-imagescnan-m-v2-1-aug-2009

Bruno
Erica B
2012-05-29 16:27:05 UTC
Permalink
Thanks for the comments,

I'd like to see your approach, Maciej. However, in my searching, I've found that the imagesc solutions won't work for pcolorm due to differences between using map axis versus regular cartesian.

Thus, I don't think Bruno's solution will work either, as it uses imagesc, and because I need map axes (with lat lon labels, political boundaries etc) I have to use one of the mapping toolbox functions (pcolorm, surfm).

But I am open to trying anything.

Thanks!

EB
Post by Bruno Luong
http://www.mathworks.com/matlabcentral/fileexchange/20516-imagescnan-m-v2-1-aug-2009
Bruno
Maciej
2012-05-30 09:04:07 UTC
Permalink
Post by Erica B
Thanks for the comments,
I'd like to see your approach, Maciej. However, in my searching, I've found that the imagesc solutions won't work for pcolorm due to differences between using map axis versus regular cartesian.
Thus, I don't think Bruno's solution will work either, as it uses imagesc, and because I need map axes (with lat lon labels, political boundaries etc) I have to use one of the mapping toolbox functions (pcolorm, surfm).
But I am open to trying anything.
Thanks!
EB
Post by Bruno Luong
http://www.mathworks.com/matlabcentral/fileexchange/20516-imagescnan-m-v2-1-aug-2009
Bruno
Hi EB
As you asked me:

imagesc(A) % A is a matrix storing data picture
hold on;
% lets get rid of NaNs
for n=1:size(A,2) % loop through x coord
for m=1:size(A,1) % loop through y coord
if (isnan(A(m,n)))
rectangle('Position',[n-0.5 m-0.5 1 1],'FaceColor',[.202 .202 .202],'LineStyle','none');
end
end
end

In this case rectangle function plots grey rectangle ([.202 .202 .202] this is RGB values, you can change it to yours needs). The size was adjust it that way to exchange NaN pixel, moreover 'none' means rectangle without edge line.

Regards
Maciek

Erica B
2012-05-29 16:32:07 UTC
Permalink
Also of interest to some of you...

I had been plotting with pcolorm using lat-lon corners, such that the dimension of the lat lon arrays was one greater in both dimensions than that of the data I was plotting. In this case, NaNs plot as dark blue (lowest color on colorbar).

However, if I remove the extra dimension from the lat lons such that size(lat)=size(lon)=size(data), then the NaNs plot as white. This may be an improvement for some of you over the dark blue. However, I'd still like to try and get NaNs to plot to grey, if I can.

EB
Loading...