How can I apply alpha on pcolor without altering the data

82 views (last 30 days)
Hi, I want to create a mask on my pcolor plot which will make some part of the data transparent and the rest will remain clear. I tried to do this by creating two matrixes with NaN values wherever I wanted to mask it, one is a negative of the other, making one of them transparent and placing one on the other (by simple hold on operation). I created a test.script to show does it work or better how doesn't it work: Alpha changes the shape of my mask, if you can find any solution for that I will be vey grateful.
% my real data
x=1:171; y=1:81; [X,RR]=meshgrid(x,y);
% creating mask (in reality is much more complicated)
mask=zeros(81,171);
mask(20:60,10:50)=ones(41,41);
mask(30:70,100:130)=ones(41,31);
% creating reversed mask
for n=1:81 for m=1:171 if mask(n,m)==1 revmask(n,m)=0; elseif mask(n,m)==0 revmask(n,m)=1; end end end
% creating data matrix with mask
for n=1:81 for m=1:171 if mask(n,m)==1 DD(n,m)=RR(n,m); else DD(n,m)=NaN; end end end
% creating data matrix with reversed mask
for n=1:81 for m=1:171 if revmask(n,m)==1 GG(n,m)=RR(n,m); else GG(n,m)=NaN; end end end % plotting
subplot(2,2,1)
pcolor(x,y,GG)
subplot(2,2,3)
e1=pcolor(x,y,GG); caxis([1 81]) set(e1,'facealpha',0.3)
subplot(2,2,2)
pcolor(x,y,DD) caxis([1 81])
subplot(2,2,4)
e1=pcolor(x,y,GG); set(e1,'Facealpha',0.3) caxis([1 81]) hold on e2=pcolor(x,y,DD); set(e2,'facealpha',1) hold off

Answers (2)

Supreeth Subbaraya
Supreeth Subbaraya on 13 Aug 2014
This seems to be an issue with the renderer. Change the renderer to zbuffer or painters and see if the result is what you expect. To change the renderer use the set command.
set(gcf,'renderer','zbuffer')
Note that the transparency is not supported by the zbuffer or painters.
And if you do not need the mesh lines on your image, you can add the following line at the end of your code.
shading flat
  1 Comment
Maciej
Maciej on 14 Aug 2014
But what I need is actually transparency, and what is altering my image is the alpha function, so it doesn't make sense to change to different rendering mode if I can't use alpha then.

Sign in to comment.


Supreeth Subbaraya
Supreeth Subbaraya on 14 Aug 2014
Before the "hold off" line in your code instead of the command,
set(e2,'facealpha',1)
I guess it should be,
set(e2,'facealpha',0.3)
The transparency value should be 0.3 instead of 1.

Community Treasure Hunt

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

Start Hunting!