Changing Transparency of Images, Patches or Surfaces
This example shows how to modify transparency of images, patches and surfaces.
Transparency for All Objects in Axes
Transparency values are referred to as alpha values. Use the
alpha function to set the transparency for all image, patch, and surface objects in the current axes. Specify a transparency value between 0 (fully transparent) and 1 (fully opaque).
t = 0:0.1:2*pi; x = sin(t); y = cos(t); figure patch(x,y,'r') patch(x+0.8,y,'g') patch(x+0.4,y+0.8,'b') axis square tight alpha(0.3)
Transparency for Individual Surfaces
The transparency of a surface is defined by its
AlphaData property. Set the alpha data as either a scalar value or a matrix of values specifying the transparency of each vertex of the surface. The
FaceAlpha property indicates how the transparency of the surface faces are determined from vertex transparency.
[X,Y,Z] = peaks(20); s2 = surf(X,Y,Z); s2.AlphaData = gradient(Z); s2.FaceAlpha = 'flat';
Transparency for Individual Images
Like surfaces, the transparency of an image is also defined by its
AlphaData property. For images, set the alpha data as either a scalar value or a matrix of values specifying the transparency of each element in the image data.
For example, use transparency to overlay two images. First, display the image of the Earth.
earth = imread('landOcean.jpg'); image(earth) axis image
Then, add a cloud layer to the image of the Earth using transparency.
clouds = imread('cloudCombined.jpg'); image(earth) axis image hold on im = image(clouds); im.AlphaData = max(clouds,,3); hold off
Transparency for Individual Patches
The transparency of a patch is defined by its
FaceVertexAlphaData properties. For constant transparency across the entire patch, set the
FaceVertexAlphaData to a constant between 0 (fully transparent) and 1 (fully opaque), and set the
FaceAlpha property to
cla p1 = patch(x,y,'r'); axis square tight p1.FaceVertexAlphaData = 0.2; p1.FaceAlpha = 'flat' ;
For transparency that varies across the patch, set the
FaceVertexAlphaData to a matrix of values specifying the transparency at each vertex or each face of the patch. The
FaceAlpha property then indicates how the face transparencies are determined using the
FaceVertexAlphaData. If alpha data is specified for vertices,
FaceAlpha must be set to
p1.FaceVertexAlphaData = x'; p1.FaceAlpha = 'interp';
Transparency with Texture Mapping
Texture mapping maps a 2-D image onto a 3-D surface. An image can be mapped to a surface by setting the
CData property to the image data and setting the
FaceColor property to be
This example creates a 3-D view of the earth and clouds. It creates spherical surfaces and uses texture mapping to map the images of the earth and clouds onto the surfaces.
[px,py,pz] = sphere(50); sEarth = surface(py, px ,flip(pz)); sEarth.FaceColor = 'texturemap'; sEarth.EdgeColor = 'none'; sEarth.CData = earth; hold on sCloud = surface(px*1.02,py*1.02,flip(pz)*1.02); sCloud.FaceColor = 'texturemap'; sCloud.EdgeColor = 'none'; sCloud.CData = clouds; sCloud.FaceAlpha = 'texturemap'; sCloud.AlphaData = max(clouds,,3); hold off view([80 2]) daspect([1 1 1]) axis off tight
The images used in this example are from Visible Earth.
Credit: NASA Goddard Space Flight Center Image by Reto Stöckli (land surface, shallow water, clouds). Enhancements by Robert Simmon (ocean color, compositing, 3D globes, animation). Data and technical support: MODIS Land Group; MODIS Science Data Support Team; MODIS Atmosphere Group; MODIS Ocean Group Additional data: USGS EROS Data Center (topography); USGS Terrestrial Remote Sensing Flagstaff Field Center (Antarctica); Defense Meteorological Satellite Program (city lights).