add transparency to a contourf

39 views (last 30 days)
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA on 27 Nov 2022
Edited: DGM on 27 Nov 2022
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?

Accepted Answer

Star Strider
Star Strider on 27 Nov 2022
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 Comments
Star Strider
Star Strider on 27 Nov 2022
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.

Sign in to comment.

More Answers (1)

DGM
DGM on 27 Nov 2022
Edited: DGM on 27 Nov 2022
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:

Community Treasure Hunt

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

Start Hunting!

Translated by