mesh
Mesh surface plot
Description
mesh( creates a mesh plot and uses
the column and row indices of the elements in Z)Z as the
x- and y-coordinates.
mesh( plots into
the axes specified by ax,___)ax instead of the current axes. Specify
the axes as the first input argument.
mesh(___,
specifies surface properties using one or more name-value pair arguments. For
example, Name,Value)'FaceAlpha',0.5 creates a semitransparent mesh
plot.
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.
Examples
Create three matrices of the same size. Then plot them as a mesh plot. The plot uses Z for both height and color.
[X,Y] = meshgrid(-8:.5:8); R = sqrt(X.^2 + Y.^2) + eps; Z = sin(R)./R; mesh(X,Y,Z)

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 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 in C correspond to the colors in the colormap.
[X,Y] = meshgrid(-8:.5:8); R = sqrt(X.^2 + Y.^2) + eps; Z = sin(R)./R; C = X.*Y; mesh(X,Y,Z,C) colorbar

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 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 component for each color, the second page indicates the green component, and the third page indicates the blue component.
[X,Y,Z] = peaks(25); CO(:,:,1) = zeros(25); % red CO(:,:,2) = ones(25).*linspace(0.5,0.6,25); % green CO(:,:,3) = ones(25).*linspace(0,1,25); % blue mesh(X,Y,Z,CO)

Create a semitransparent mesh surface by specifying the FaceAlpha name-value pair with 0.5 as the value. To allow further modifications, assign the surface object to the variable s.
[X,Y] = meshgrid(-5:.5:5); Z = Y.*sin(X) - X.*cos(Y); s = mesh(X,Y,Z,'FaceAlpha','0.5')

s =
Surface with properties:
EdgeColor: 'flat'
LineStyle: '-'
FaceColor: [1 1 1]
FaceLighting: 'none'
FaceAlpha: 0.5000
XData: [21×21 double]
YData: [21×21 double]
ZData: [21×21 double]
CData: [21×21 double]
Show all properties
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.
s.FaceColor = 'flat';
Input Arguments
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).
You can use the meshgrid function to create
X and Y matrices.
The XData property of the surface object stores the
x-coordinates.
Example: X = 1:10
Example: X = [1 2 3; 1 2 3; 1 2 3]
Example: [X,Y] = meshgrid(-5:0.5:5)
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration
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).
You can use the meshgrid function to create
the X and Y matrices.
The YData property of the surface object stores the
y-coordinates.
Example: Y = 1:10
Example: Y = [1 1 1; 2 2 2; 3 3 3]
Example: [X,Y] = meshgrid(-5:0.5:5)
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration
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.
The ZData property of the surface object stores the
z-coordinates.
Example: Z = [1 2 3; 4 5 6]
Example: Z = sin(x) + cos(y)
Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration
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
Cas a matrix. For each grid point on the mesh surface,Cindicates a color in the colormap. TheCDataMappingproperty of the surface object controls how the values inCcorrespond to colors in the colormap.To use truecolor colors, specify
Cas an array of RGB triplets.
For more information, see Differences Between Colormaps and Truecolor.
The CData property of the surface object stores the
color array. For additional control over the surface coloring, use the
FaceColor and EdgeColor
properties.
Axes to plot into, specified as an Axes or PolarAxes
object. If you do not specify the axes, then mesh plots into
the current axes or creates an Axes object (Cartesian 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.
Example: mesh(X,Y,Z,'FaceAlpha',0.5) creates a semitransparent
mesh plot.
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 in this table.
| Value | Description |
|---|---|
'none' | Do not draw the edges. |
'flat' | Use a different color for each edge based on the values
in the
|
'interp' |
Use interpolated coloring for each edge based on the values in the
|
| 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
|
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 from0toF. 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.
| Color Name | Short Name | RGB Triplet | Hexadecimal Color Code | Appearance |
|---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" |
|
"green" | "g" | [0 1 0] | "#00FF00" |
|
"blue" | "b" | [0 0 1] | "#0000FF" |
|
"cyan"
| "c" | [0 1 1] | "#00FFFF" |
|
"magenta" | "m" | [1 0 1] | "#FF00FF" |
|
"yellow" | "y" | [1 1 0] | "#FFFF00" |
|
"black" | "k" | [0 0 0] | "#000000" |
|
"white" | "w" | [1 1 1] | "#FFFFFF" |
|
This table lists the default color palettes for plots in the light and dark themes.
| Palette | Palette Colors |
|---|---|
Before R2025a: Most plots use these colors by default. |
|
|
|
You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.
RGB = orderedcolors("gem");
H = rgb2hex(RGB);Before R2023b: Get the RGB triplets using RGB =
get(groot,"FactoryAxesColorOrder").
Before R2024a: Get the hexadecimal color codes using H =
compose("#%02X%02X%02X",round(RGB*255)).
Line style, specified as one of the options listed in this table.
| Line Style | Description | Resulting Line |
|---|---|---|
"-" | Solid line |
|
"--" | Dashed line |
|
":" | Dotted line |
|
"-." | Dash-dotted line |
|
"none" | No line | No line |
Face color, specified as one of the values in this table.
| Value | Description |
|---|---|
'flat' | Use a different color for each face based on the values
in the
|
'interp' |
Use interpolated coloring for each face based on the values in the
|
| 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
|
'texturemap' | Transform the color data in CData so that
it conforms to the surface. |
'none' | Do not draw the faces. |
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 from0toF. 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.
| Color Name | Short Name | RGB Triplet | Hexadecimal Color Code | Appearance |
|---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" |
|
"green" | "g" | [0 1 0] | "#00FF00" |
|
"blue" | "b" | [0 0 1] | "#0000FF" |
|
"cyan"
| "c" | [0 1 1] | "#00FFFF" |
|
"magenta" | "m" | [1 0 1] | "#FF00FF" |
|
"yellow" | "y" | [1 1 0] | "#FFFF00" |
|
"black" | "k" | [0 0 0] | "#000000" |
|
"white" | "w" | [1 1 1] | "#FFFFFF" |
|
This table lists the default color palettes for plots in the light and dark themes.
| Palette | Palette Colors |
|---|---|
Before R2025a: Most plots use these colors by default. |
|
|
|
You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.
RGB = orderedcolors("gem");
H = rgb2hex(RGB);Before R2023b: Get the RGB triplets using RGB =
get(groot,"FactoryAxesColorOrder").
Before R2024a: Get the hexadecimal color codes using H =
compose("#%02X%02X%02X",round(RGB*255)).
Face transparency, specified as one of these values:
Scalar in range
[0,1]— Use uniform transparency across all the faces. A value of1is fully opaque and0is completely transparent. Values between0and1are semitransparent. This option does not use the transparency values in theAlphaDataproperty.'flat'— Use a different transparency for each face based on the values in theAlphaDataproperty. The transparency value at the first vertex determines the transparency for the entire face. First you must specify theAlphaDataproperty as a matrix the same size as theZDataproperty. TheFaceColorproperty also must be set to'flat'.'interp'— Use interpolated transparency for each face based on the values inAlphaDataproperty. The transparency varies across each face by interpolating the values at the vertices. First you must specify theAlphaDataproperty as a matrix the same size as theZDataproperty. TheFaceColorproperty also must be set to'interp'.'texturemap'— Transform the data inAlphaDataso that it conforms to the surface.
Effect of light objects on faces, specified as one of these values:
'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.'none'— Do not apply light from light objects to the faces.
To add a light object to the axes, use the light function.
Note
The 'phong' value has been removed. Use 'gouraud' instead.
Tips
Extended Capabilities
The mesh function
supports GPU array input with these usage notes and limitations:
This function accepts GPU arrays, but does not run on a GPU.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Usage notes and limitations:
This function operates on distributed arrays, but executes in the client MATLAB®.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006aCreate mesh plots in polar coordinates by specifying a
PolarAxes object as the first argument.
See Also
Functions
Properties
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)





