MATLAB Answers

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

조회 수: 188(최근 30일)
TheoBoveri
TheoBoveri 2020년 4월 2일
댓글: Alan Meier 2020년 7월 16일
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...

답변(1개)

darova
darova 2020년 4월 2일
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
  댓글 수: 11
Alan Meier
Alan Meier 2020년 7월 16일
I'm using R2018 a and when I run your answer code snippet, I get the same result as TheoBoveri. When I change the code as in the second comment I get the same error as TheoBoveri again.
Using the code from the next comment and the provided .fig file I get the following result because the length(h1) is empty for me as well:
Anf finally the result for the code in the last comment:
f1 =
Figure (1) with properties:
Number: 1
Name: ''
Color: [0.9400 0.9400 0.9400]
Position: [704 518 560 420]
Units: 'pixels'
Show all properties
ans =
'figure'
h0 =
Axes with properties:
XLim: [-3 3]
YLim: [-3 3]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [0.1300 0.1100 0.7750 0.8150]
Units: 'normalized'
Show all properties
ans =
'axes'
h0 =
Contour with properties:
LineColor: 'flat'
LineStyle: '-'
LineWidth: 0.5000
Fill: 'off'
LevelList: [-6 -4 -2 0 2 4 6]
XData: [20×20 double]
YData: [20×20 double]
ZData: [20×20 double]
Show all properties
ans =
'contour'
h1 =
0×0 empty GraphicsPlaceholder array.
ans =
[]

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by