surface - Primitive surface plot - MATLAB (original) (raw)

Syntax

Description

surface([X](#mw%5F8e41d75c-2d91-4ce5-a031-67968b72dc07),[Y](#mw%5F4d70d4d6-e7be-4551-bb6d-02a11ddd45e4),[Z](#mw%5Fac3abbb8-7942-4b4f-baec-e6fadcc505de)) creates a primitive, three-dimensional surface plot. The function plots the values in matrix Z as heights above a grid in the_x_-y plane defined byX and Y. The color of the surface varies according to the heights specified by Z.

Unlike the surf function, the primitivesurface function does not call newplot before plotting and does not respect the value of the NextPlot property for the figure or axes. Instead, it adds the surface plot to the current axes without deleting other graphics objects or resetting axes properties.

example

surface([X](#mw%5F8e41d75c-2d91-4ce5-a031-67968b72dc07),[Y](#mw%5F4d70d4d6-e7be-4551-bb6d-02a11ddd45e4),[Z](#mw%5Fac3abbb8-7942-4b4f-baec-e6fadcc505de),[C](#mw%5Fd64a35c5-4287-4c45-af8f-41f697503175)) additionally specifies the surface color.

example

surface([Z](#mw%5Fac3abbb8-7942-4b4f-baec-e6fadcc505de)) creates a primitive surface plot and uses the column and row indices of the elements inZ as the _x_- and_y_-coordinates.

surface([Z](#mw%5Fac3abbb8-7942-4b4f-baec-e6fadcc505de),[C](#mw%5Fd64a35c5-4287-4c45-af8f-41f697503175)) additionally specifies the surface color.

surface([ax](#f67-521390%5Fsep%5Fmw%5F4edf80de-1d31-452b-bde6-1d90f125b88d),___) plots into the axes specified by ax instead of the current axes. Specify the axes as the first input argument.

surface(___,[Name,Value](#namevaluepairarguments)) specifies surface properties using one or more name-value pair arguments. For example, 'FaceAlpha',0.5 creates a semitransparent surface.

example

s = surface(___) returns the chart primitive surface object. Use s to modify the surface after it is created. For a list of properties, see Surface Properties.

example

Examples

collapse all

Create three matrices of the same size. Then plot them as a surface. The surface uses Z for both height and color.

[X,Y] = meshgrid(1:0.5:10,1:20); Z = sin(X) + cos(Y); surface(X,Y,Z)

Figure contains an axes object. The axes object contains an object of type surface.

By default, surface displays in the axes using a two-dimensional view. Change the axes to a three-dimensional view.

Figure contains an axes object. The axes object contains an object of type surface.

Specify the colors for a surface 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, and set the view of the plot to the default three-dimensional view.

[X,Y] = meshgrid(1:0.5:10,1:20); Z = sin(X) + cos(Y); C = X.*Y; surface(X,Y,Z,C) colorbar view(3)

Figure contains an axes object. The axes object contains an object of type surface.

Create a semitransparent 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 = surface(X,Y,Z,'FaceAlpha',0.5); view(3)

Figure contains an axes object. The axes object contains an object of type surface.

Use s to access and modify properties of the surface object after it is created. For example, hide the edges by setting the EdgeColor property.

Figure contains an axes object. The axes object contains an object of type surface.

Create a surface and display an image along it.

Create three matrices of the same size.

Load a data set containing an image of the Earth. The image data appears in a workspace variable X, and the associated colormap appears in map.

Your variables are:

X map pX pY pZ

Create a surface plot and display the image along the surface. Since the surface data pZ and the color data X have different sizes, set the surface FaceColor to 'texturemap'. Set the view of the plot to the default three-dimensional view.

surface(pX,pY,pZ,X,'FaceColor','texturemap', ... 'EdgeColor','none','CDataMapping','direct') colormap(map) view(3)

Figure contains an axes object. The axes object contains an object of type surface.

Input Arguments

collapse all

_x_-coordinates, specified as a matrix the same size asZ, or as a vector with length n, where [m,n] = size(Z). If you do not specify values forX and Y,surface uses the vectors (1:n) and (1:m).

You can use the meshgrid function to createX 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 asZ or as a vector with length m, where [m,n] = size(Z). If you do not specify values forX and Y,surface 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 surface plot at each_x_-y coordinate. If you do not specify the colors, then Z also specifies the surface 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 anm-by-n-by-3 array of RGB triplets, where Z ism-by-n.

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 theFaceColor and EdgeColor properties.

Axes to plot into, specified as an Axes or PolarAxes object. If you do not specify the axes, then surface plots into the current axes or creates an Axes object (Cartesian axes).

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=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: surface(X,Y,Z,'FaceAlpha',0.5,'EdgeColor','none') creates a semitransparent surface with no edges drawn.

Note

The properties listed here are only a subset. For a full list, see Surface Properties.

Version History

Introduced before R2006a

expand all

Create surface plots in polar coordinates by specifying aPolarAxes object as the first argument.