Clear Filters
Clear Filters

Make NaNs transparent with pcolor

56 views (last 30 days)
V.D-C
V.D-C on 27 Feb 2020
Answered: V.D-C on 27 Feb 2020
Hello,
I have a matrix with NaNs and other values. I plot in the background a land mask on which the values overlay. But, because of this the NaNs also overlay the background and I end up not seeing it anymore. I would therefore like to make the NaNs transparent so we can see the background.
So far I was using imagesc but because of a georeference issue, pcolor is more adapted. I had the following code to maks NaNs transparent with imagesc:
f2 = figure('Visible',true);
% Plot the background image
ax1 = axes;
imagesc(ax1, demx, demy, maskLand);
hold on;
% Plot the overlay image
ax2 = axes;
% Make NaNs transparent
imAlpha = ones(size(overlay));
imAlpha(isnan(overlay)) = 0;
imagesc(ax2, demx, demy, overlay, 'AlphaData', imAlpha);
Now, with pcolor I have been setting:
pcolor(maskLand...)
hold on
e1 = pcolor(overlay)
set(e1,'facealpha',0.3)
But it sets the whole overlay as transparent while I only want the NaNs of the overlay to be transparent. What can I specify to make this happen ?
Have a nice day !
  2 Comments
J. Alex Lee
J. Alex Lee on 27 Feb 2020
I'm not sure why your first strategy of setting the alphadata doesn't work, as long as you are placing the overlay in the same axes as the background (ax1 rather than ax2).
f2 = figure('Visible',true);
% Plot the background image
ax = axes("NextPlot","add");
imagesc(ax, demx, demy, maskLand);
% Plot the overlay image
% Make NaNs transparent
imAlpha = 0.3*ones(size(overlay)); % want to make it translucent?
imAlpha(isnan(overlay)) = 0;
imagesc(ax, demx, demy, overlay, 'AlphaData', imAlpha);
Or if you are trying to stack axes, you would need to make sure the axes themselves do not have a color by setting their "Color" to "none"
V.D-C
V.D-C on 27 Feb 2020
Thank you for your answer !
I didn't keep the first strategy because by using imagesc, I use a georeferenced system that is tilted and at a specific scale.
I wanted to plot everything in a UTM coordinate system (and have the figures straight) but it looks like I have to go through pcolor which is able to handle inputs as matrices (for example, pcolor(x,y,data) with x & y size 330x300).
And because of the huge amount of data involved, I can't put my detailed code in here without importing everything.

Sign in to comment.

Accepted Answer

V.D-C
V.D-C on 27 Feb 2020
Ok I managed to find where it was blocking everything.
The issue is that I was setting a black background for the second figure. To make the overlay disappear for NaNs values, then the solution is:
figure()
ax1 = axes;
pcolor(ax1,lat,lon,mask)
set(gca, 'Color', 'black'); %sets black background
colormap(ax1,cm_mask); %colormap I created which makes the land mask brown
shading flat %get rid of the lines on the figure
hold on
ax2 = axes;
imAlpha = ones(size(smb));
imAlpha(isnan(smb)) = 0; %create NaN mask the size of the overlay
e1 = pcolor(ax2,lat,lon,overlay);
set(e1,'alphadata',0) %make NaNs transparent. CAUTION: Use "Alphadata" and not "facealpha" or everything
%will be transparent
colormap(ax2,cm_overlay) %colormap I created for the overlay
shading flat
hold on
set(gca, 'Color', 'none'); %IMPORTANT: set the second background as transparent otherwise it covers the land mask
axis tight
My mistake was to use a black background and "facealpha".

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!