add transparency to a contourf

조회 수: 218 (최근 30일)
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA 2022년 11월 27일
편집: DGM 2022년 11월 27일
I have an image (a DEM.tif) inserted in the above script where I plotted a map created with contourf. the code is:
[AA,EE]=geotiffread('dem_eu_rs.tif');
geoshow(AA,EE);
hold on
contourf(XX,YY,ZZ,500,'linecolor','none');
colormap jet
hold off
I should make the map transparent and therefore the contourf and not the .tif which instead must remain under the map.
I tried to write
s=contourf(XX,YY,ZZ,500,'linecolor','none');
colormap jet
hold off
alpha(s,.5)
but it made me transparent the DEM below instead of the variable s.
Can anyone help me?

채택된 답변

Star Strider
Star Strider 2022년 11월 27일
You have to use the handle to the contourf plot to set the transparency —
[X,Y,Z] = peaks(50);
figure
contourf(X, Y, Z) % Retunr No Information
colormap(turbo)
figure
[c,h] = contourf(X, Y, Z); % Return Contour Matrix ('c') & Handle ('h')
colormap(turbo)
h.FaceAlpha = 0.5; % Set Transparency ('FaceAlpha') Value
You can use the same approach to set other properties to values different than the default values.
.
  댓글 수: 3
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA 2022년 11월 27일
to be precise I have matlab R2021b!!!
Star Strider
Star Strider 2022년 11월 27일
It will likely be best for you to use contourfm rather than contourf since you are using the Mapping Toolbox functions. I do not have access to earlier versions of the Mapping Toolbox, since I do not have it myself and so can only use it with the online Run feature here.
Looking through the online documentation for the R2021b contourf function, there is no mention of a 'FaceAlpha' property. It might be possible to use the patch or fill functions with contour to create a contour plot that has 'FaceAlpha' properties, however that would likely be difficult, although using polyshape could be an option.
It will be necessary for you to upgrade to R2022b to use the 'FaceAlpha' contourf (and likely also the contourfm) property.

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

추가 답변 (1개)

DGM
DGM 2022년 11월 27일
편집: DGM 2022년 11월 27일
The FaceAlpha and EdgeAlpha properties are new in R2022b.
I'm not familiar with cotourfm() or mapping tools, so I can't comment on that. If you just have an image and an overlaid contourf() that you want to be semitransparent, you can still do that.
One possible way is to simply set the alpha of the descendant objects that make up the contour plot. That's verbose, but it works back to R2014b. This also allows you to set the transparency of the contour levels individually if desired.
The other way is to flip the stacking order and make the image transparent instead. If the image and plot cover each other completely, this is pretty simple to do. If the contourf() plot doesn't cover the entire image area, you'll have to create a second copy of the image to use as a background behind the contourf(). If your image is single-channel, you'll have to expand it.
See the following answers:
Editing primitives:
Editing primitives and doing inverted stacking:
Comment on using grayscale images with colormapped graphics objects:

카테고리

Help CenterFile Exchange에서 Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by