add transparency to a contourf

162 vues (au cours des 30 derniers jours)
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA le 27 Nov 2022
Modifié(e) : DGM le 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?

Réponse acceptée

Star Strider
Star Strider le 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 commentaires
ELISABETTA BILLOTTA
ELISABETTA BILLOTTA le 27 Nov 2022
to be precise I have matlab R2021b!!!
Star Strider
Star Strider le 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.

Connectez-vous pour commenter.

Plus de réponses (1)

DGM
DGM le 27 Nov 2022
Modifié(e) : DGM le 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:

Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by