Mesh Surface Plot - MATLAB Mesh
Mesh Surface Plot - MATLAB Mesh
mesh
Mesh surface plot
Syntax
mesh(X,Y,Z)
mesh(Z)
mesh(Z,C)
mesh( ___ ,C)
mesh(ax, ___ )
mesh( ___ ,Name,Value)
s = mesh( ___ )
Description
mesh(X,Y,Z) creates a mesh plot, which is a three-dimensional surface that has solid edge colors and no example
face colors. The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X
and Y. The edge colors vary according to the heights specified by Z.
mesh(Z) creates a mesh plot and uses the column and row indices of the elements in Z as the x- and y-
coordinates.
example
mesh( ___ ,C) additionally specifies the color of the edges.
mesh(ax, ___ ) plots into the axes specified by ax instead of the current axes. Specify the axes as the first
input argument.
example
mesh( ___ ,Name,Value) specifies surface properties using one or more name-value pair arguments. For
example, 'FaceAlpha',0.5 creates a semitransparent mesh plot.
example
s = mesh( ___ ) returns the chart surface object. Use s to modify the mesh plot after it is created. For a
list of properties, see Surface Properties.
https://www.mathworks.com/help/matlab/ref/mesh.html 1/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
Create three matrices of the same size. Then plot them as a mesh plot. The plot
Try This Example
uses Z for both height and color.
Copy Command
Specify the colors for a mesh plot by including a fourth matrix input, C. The mesh
plot uses Z for height and C for color. Specify the colors using a colormap, which Try This Example
uses single numbers to stand for colors on a spectrum. When you use a colormap,
C is the same size as Z. Add a color bar to the graph to show how the data values
Copy Command
in C correspond to the colors in the colormap.
https://www.mathworks.com/help/matlab/ref/mesh.html 2/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
Specify the colors for a mesh plot by including a fourth matrix input, CO. The mesh
plot uses Z for height and CO for color. Specify the colors using truecolor, which Try This Example
uses triplets of numbers to stand for all possible colors. When you use truecolor, if
Z is m-by-n, then CO is m-by-n-by-3. The first page of the array indicates the red
Copy Command
component for each color, the second page indicates the green component, and
the third page indicates the blue component.
https://www.mathworks.com/help/matlab/ref/mesh.html 3/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
Copy Command
https://www.mathworks.com/help/matlab/ref/mesh.html 4/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
s =
Surface with properties:
EdgeColor: 'flat'
LineStyle: '-'
FaceColor: [1 1 1]
FaceLighting: 'none'
FaceAlpha: 0.5000
XData: [21x21 double]
YData: [21x21 double]
ZData: [21x21 double]
CData: [21x21 double]
Use s to access and modify properties of the mesh plot after it is created. For example, add color to the face of the
mesh plot by setting the FaceColor property.
https://www.mathworks.com/help/matlab/ref/mesh.html 5/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
X — x-coordinates
matrix | vector
x-coordinates, specified as a matrix the same size as Z, or as a vector with length n, where [m,n] = size(Z). If
you do not specify values for X and Y, mesh uses the vectors (1:n) and (1:m).
Example: X = 1:10
Example: X = [1 2 3; 1 2 3; 1 2 3]
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical |
datetime | duration
Y — y-coordinates
matrix | vector
y-coordinates, specified as a matrix the same size as Z or as a vector with length m, where [m,n] = size(Z). If you
do not specify values for X and Y, mesh uses the vectors (1:n) and (1:m).
https://www.mathworks.com/help/matlab/ref/mesh.html 6/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
You can use the meshgrid function to create the X and Y matrices.
Example: Y = 1:10
Example: Y = [1 1 1; 2 2 2; 3 3 3]
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical |
datetime | duration
Z — z-coordinates
matrix
z -coordinates, specified as a matrix. Z must have at least two rows and two columns.
Z specifies the height of the mesh plot at each x-y coordinate. If you do not specify the colors, then Z also specifies
the mesh edge colors.
Example: Z = [1 2 3; 4 5 6]
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical |
datetime | duration
C — Color array
matrix | m-by-n-by-3 array of RGB triplets
Color array, specified as an m-by-n matrix of colormap indices or as an m-by-n-by-3 array of RGB triplets, where Z is m-
by-n.
To use colormap colors, specify C as a matrix. For each grid point on the mesh surface, C indicates a color in the
colormap. The CDataMapping property of the surface object controls how the values in C correspond to colors
in the colormap.
The CData property of the surface object stores the color array. For additional control over the surface coloring, use
the FaceColor and EdgeColor properties.
ax — Axes to plot in
axes object
https://www.mathworks.com/help/matlab/ref/mesh.html 7/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
Axes to plot in, specified as an axes object. If you do not specify the axes, then mesh plots into the current axes.
Name-Value Arguments
Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and
Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs
does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name in quotes.
Note
The properties listed here are only a subset. For a full list, see Surface Properties.
Edge line color, specified as one of the values listed here. The default color of [0 0 0] corresponds to black edges.
Value Description
'flat' Use a different color for each edge based on the values in
the CData property. First you must specify the CData
property as a matrix the same size as ZData. The color
value at the first vertex of each face (in the positive x and y
directions) determines the color for the adjacent edges. You
cannot use this value when the EdgeAlpha property is set to
'interp'.
https://www.mathworks.com/help/matlab/ref/mesh.html 8/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
Value Description
'interp' Use interpolated coloring for each edge based on the values
in the CData property. First you must specify the CData
property as a matrix the same size as ZData. The color
varies across each edge by linearly interpolating the color
values at the vertices. You cannot use this value when the
EdgeAlpha property is set to 'flat'.
RGB triplet, hexadecimal color code, or color name Use the specified color for all the edges. This option does
not use the color values in the CData property.
RGB triplets and hexadecimal color codes are useful for specifying custom colors.
An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue
components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].
A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by
three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color
codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.
Alternatively, you can specify some common colors by name. This table lists the named color options, the
equivalent RGB triplets, and hexadecimal color codes.
Hexadecimal Color
Color Name Short Name RGB Triplet Appearance
Code
https://www.mathworks.com/help/matlab/ref/mesh.html 9/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
Hexadecimal Color
Color Name Short Name RGB Triplet Appearance
Code
Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.
https://www.mathworks.com/help/matlab/ref/mesh.html 10/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
Value Description
'flat' Use a different color for each face based on the values in
the CData property. First you must specify the CData
property as a matrix the same size as ZData. The color
value at the first vertex of each face (in the positive x and y
directions) determines the color for the entire face. You
cannot use this value when the FaceAlpha property is set to
'interp'.
'interp' Use interpolated coloring for each face based on the values
in the CData property. First you must specify the CData
property as a matrix the same size as ZData. The color
varies across each face by interpolating the color values at
the vertices. You cannot use this value when the FaceAlpha
property is set to 'flat'.
https://www.mathworks.com/help/matlab/ref/mesh.html 11/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
Value Description
RGB triplet, hexadecimal color code, or color name Use the specified color for all the faces. This option does
not use the color values in the CData property.
An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue
components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7].
A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (#) followed by
three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Thus, the color
codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.
Alternatively, you can specify some common colors by name. This table lists the named color options, the
equivalent RGB triplets, and hexadecimal color codes.
Hexadecimal Color
Color Name Short Name RGB Triplet Appearance
Code
Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB uses in many types of plots.
https://www.mathworks.com/help/matlab/ref/mesh.html 12/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
Scalar in range [0,1] — Use uniform transparency across all the faces. A value of 1 is fully opaque and 0 is
completely transparent. Values between 0 and 1 are semitransparent. This option does not use the transparency
values in the AlphaData property.
'flat' — Use a different transparency for each face based on the values in the AlphaData property. The
transparency value at the first vertex determines the transparency for the entire face. First you must specify the
AlphaData property as a matrix the same size as the ZData property. The FaceColor property also must be set
to 'flat'.
'interp' — Use interpolated transparency for each face based on the values in AlphaData property. The
transparency varies across each face by interpolating the values at the vertices. First you must specify the
AlphaData property as a matrix the same size as the ZData property. The FaceColor property also must be set
to 'interp'.
'flat' — Apply light uniformly across each face. Use this value to view faceted objects.
'gouraud' — Vary the light across the faces. Calculate the light at the vertices and then linearly interpolate the
light across the faces. Use this value to view curved surfaces.
https://www.mathworks.com/help/matlab/ref/mesh.html 13/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
'none' — Do not apply light from light objects to the faces.
To add a light object to the axes, use the light function.
Note
Tips
To remove hidden lines from the plot, use the hidden function.
To control the color shading of the plot surfaces, use the shading function.
To create a three-dimensional surface with face colors, use the surf function.
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing
Toolbox™.
Version History
Introduced before R2006a
See Also
Functions
colormap | pcolor | meshgrid | imagesc | meshc | view | surf | meshz | griddata | shading
Properties
Surface Properties
Topics
Representing Data as a Surface
How Surface Plot Data Relates to a Colormap
Reuse code by moving it into a function that you can call multiple times.
Learn more
https://www.mathworks.com/help/matlab/ref/mesh.html 14/15
4/2/24, 11:52 PM Mesh surface plot - MATLAB mesh
https://www.mathworks.com/help/matlab/ref/mesh.html 15/15