MATLAB Answers

Adjusting the transparency of a contour plot using a gradient of alpha values

조회 수: 31(최근 30일)
TheoBoveri
TheoBoveri 2 Apr 2020
댓글: darova 8 Apr 2020
Hi,
I'm drawing a contourplot .
hLines = 5;
[~,h] = contour(X, Y, Z,hLines,'Fill','on');
For the plot I would like to adjust the alpha values decreasingly. Meaning, the face of the lower Z-Values is almost transparent, whereas the peak should be completely opaque.
If found a possible solution here submitted. And so far it works fine. But here comes the problem. If I run the code, everythings looks fine, but if I adjust the contour plot e.g. by moving it etc. it goes back to it's default alpha values! It seems as if matlab sort of 'overwrites' my changes the second it refreshes the plot.
Does anyone know any way to adjust the alpha values of the faces and make them keep the transparencies?
Here is the entire section that I'm using.
hLines = 5;
[~,h] = contour(X, Y, Z,hLines,'Fill','on');
drawnow;
title('Inverse 2D Laplace Spectrum','FontSize',20,'FontWeight','bold','Interpreter','none');
ylh = ylabel('T1-Values [s]','FontSize',12,'FontWeight','bold');
xlh = xlabel('T2-Values [s]','FontSize',12,'FontWeight','bold');
set(gca,'XScale', 'log');
set(gca,'YScale', 'log');
grid on;
grid minor;
colorbar;
hFills = h.FacePrims;
[hFills.ColorType] = deal('truecoloralpha');
AlphaGradient=exp(linspace(log(10),log(200),hLines)); %The actual values that I want to use for the alpha values
for idx = 1:numel(hFills)
hFills(idx).ColorData(4) = AlphaGradient(idx);
end
So this is how it looks like directely after the adjustments and the way it actually want it to look...
Transparent plot
And this is how it looks like if I move it just a little bit...

  댓글 수: 0

로그인 to comment.

답변(1개)

darova
darova 2 Apr 2020
Here is my today achievement
clc,clear
opengl software
n = 1000;
[X,Y,Z] = peaks(20);
% surf(X,Y,Z,'facecolor','none')
hold on
for lev = 1:2:7
[~,h] = contourf(X,Y,Z,[1 1]*lev); % create contour at some level
h1 = get(h,'children');
h2 = copyobj(h1,gca); % create copy for white surface
set(h2,'facecolor','w') % copy object - white
set(h1,'facealpha',lev/7) % facealpha
set(h1,'zdata',lev*ones(n,1)) % assign some height for surface
set(h2,'zdata',lev*ones(n,1)-0.5) % bottom white surface
end
hold off
axis vis3d

  댓글 수: 10

표시 이전 댓글 수: 7
darova
darova 7 Apr 2020
What about this?
clc,clear
close
opengl software
f1 = openfig('TestFig.fig');
h0 = get(f1,'children');
h0 = get(h0,'children');
h1 = get(h0,'children');
N = length(h1);
for i = 1:N
vv = get(h1(i),'vertices'); % last row of indices has 'NaN' values
vv(end,:) = vv(1,:); % replace 'NaN' with first row
set(h1(i),'vertices',vv);
h2 = copyobj(h1(i),gca); % create copy for white surface
set(h2,'facecolor','w') % copy object - white face
set(h1(i),'facealpha',i/N) % facealpha
zz = get(h1(i),'zdata');
set(h2,'zdata',zz-0.5) % bottom white surface
% set([h1(i) h2],'facevertexCdata',zz);
end
set(h1,'facecolor','flat')
axis vis3d
TheoBoveri
TheoBoveri 8 Apr 2020
The length(h1) turns out empty. Therefore the entire 'for' block is beeing skiped as it is 1:0.
So there is actually no processing beeing done. What is h1 supposed to be?
darova
darova 8 Apr 2020
f1 = openfig('TestFig.fig')
get(f1,'type')
h0 = get(f1,'children')
get(h0,'type')
h0 = get(h0,'children')
get(h0,'type')
h1 = get(h0,'children')
get(h1,'type')
output
f1 =
1
ans =
figure
h0 =
174.0237
ans =
axes
h0 =
175.0237
ans =
hggroup
h1 =
185.0237
% 10 more the same
ans =
'patch'
% 10 more the same
You should dig into this by yourself. Handles should be patches to manipulate with them

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by