Main Content

contour3

3-D contour plot

  • 3-D contour plot

Description

example

contour3(Z) creates a 3-D contour plot containing the isolines of matrix Z, where Z contains height values on the x-y plane. MATLAB® automatically selects the contour lines to display. The column and row indices of Z are the x and y coordinates in the plane, respectively.

example

contour3(X,Y,Z) specifies the x and y coordinates for the values in Z.

example

contour3(___,levels) specifies the contour lines to display as the last argument in any of the previous syntaxes. Specify levels as a scalar value n to display the contour lines at n automatically chosen levels (heights). To draw the contour lines at specific heights, specify levels as a vector of monotonically increasing values. To draw the contours at one height (k), specify levels as a two-element row vector [k k].

contour3(___,LineSpec) specifies the style and color of the contour lines.

contour3(___,Name,Value) specifies additional options for the contour plot using one or more name-value pair arguments. Specify the options after all other input arguments. For a list of properties, see Contour Properties.

contour3(ax,___) displays the contour plot in the target axes. Specify the axes as the first argument in any of the previous syntaxes.

M = contour3(___) returns the contour matrix M, which contains the (x, y) coordinates of the vertices at each level.

example

[M,c] = contour3(___) returns the contour matrix and the contour object c. Use c to set properties after displaying the contour plot.

Examples

collapse all

Define Z as a function of X and Y. In this case, call the sphere function to create X, Y, and Z. Then plot the contours of Z.

[X,Y,Z] = sphere(50);
contour3(X,Y,Z);

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

Define Z as a function of two variables, X and Y. Then plot the contours of Z. In this case, let MATLAB® choose the contours and the limits for the x- and y-axes.

[X,Y] = meshgrid(-5:0.25:5);
Z = X.^2 + Y.^2;
contour3(Z)

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

Now specify 50 contour levels, and display the results within the x and y limits used to calculate Z.

contour3(X,Y,Z,50)

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

Define Z as a function of two variables, X and Y. Then plot the contours at Z = [-.2 -.1 .1 .2]. Show the contour labels by setting the ShowText property to 'on'.

[X,Y] = meshgrid(-2:0.25:2);
Z = X.*exp(-X.^2-Y.^2);
contour3(X,Y,Z,[-.2 -.1 .1 .2],'ShowText','on')

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

Define Z as a function of X and Y. In this case, call the peaks function to create X, Y, and Z. Then display the contours at Z = 2.

[X,Y,Z] = peaks;
contour3(X,Y,Z,[2 2]);

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

Define Z as a function of two variables, X and Y. Plot 30 contours of Z, and then set the line width to 3.

[X,Y] = meshgrid(-2:0.0125:2);
Z = X.*exp(-X.^2-Y.^2);
[M,c] = contour3(X,Y,Z,30);
c.LineWidth = 3;

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

Input Arguments

collapse all

x-coordinates, specified as a matrix the same size as Z, or as a vector with length n, where [m,n] = size(Z). The default value of X is the vector (1:n).

When X is a matrix, the values must be strictly increasing or decreasing along one dimension and remain constant along the other dimension. The dimension that varies must be the opposite of the dimension that varies in Y. You can use the meshgrid function to create X and Y matrices.

When X is a vector, the values must be strictly increasing or decreasing.

Example: X = 1:10

Example: X = [1 2 3; 1 2 3; 1 2 3]

Example: [X,Y] = meshgrid(1:10)

The XData property of the Contour object stores the x-coordinates.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

y-coordinates, specified as a matrix the same size as Z, or as a vector with length m, where [m,n] = size(Z). The default value of Y is the vector (1:m).

When Y is a matrix, the values must be strictly increasing or decreasing along one dimension and remain constant along the other dimension. The dimension that varies must be the opposite of the dimension that varies in X. You can use the meshgrid function to create the X and Y matrices.

When Y is a vector, the values must be strictly increasing or decreasing.

Example: Y = 1:10

Example: Y = [1 1 1; 2 2 2; 3 3 3]

Example: [X,Y] = meshgrid(1:10)

The YData property of the Contour object stores the y-coordinates.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

z-coordinates, specified as a matrix. This matrix must have at least two rows and two columns, and it must contain at least two different values.

Example: Z = peaks(20)

The ZData property of the Contour object stores the z-coordinates.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Contour levels, specified as a scalar whole number or a vector. Use this argument to control the number and location of the contour lines. When you do not specify the levels, the contour3 function chooses the levels automatically.

  • To draw contour lines at n automatically chosen heights, specify levels as the scalar value n.

  • To draw the contour lines at specific heights, specify levels as a vector of monotonically increasing values.

  • To draw contour lines at a single height k, specify levels as a two-element row vector [k k].

Example: contour3(peaks,10) draws contour lines at 10 automatically chosen heights on the peaks function.

Example: contour3(peaks,[-4 0 4]) draws contour lines at 3 specific heights on the peaks function: -4, 0, and 4.

Example: contour3(peaks,[3 3]) draws contour lines to show where the height of the peaks function is 3.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Line style and color, specified as a character vector or string scalar containing characters and symbols. The characters and symbols can appear in any order. You can specify the line style, line color, or both. Marker symbols such as 'o' are ignored.

Example: '--g' is a green dashed line.

Line StyleDescriptionResulting Line
"-"Solid line

Sample of solid line

"--"Dashed line

Sample of dashed line

":"Dotted line

Sample of dotted line

"-."Dash-dotted line

Sample of dash-dotted line, with alternating dashes and dots

Color NameShort NameAppearance
'red''r'

Sample of the color red

'green''g'

Sample of the color green

'blue''b'

Sample of the color blue

'cyan' 'c'

Sample of the color cyan

'magenta''m'

Sample of the color magenta

'yellow''y'

Sample of the color yellow

'black''k'

Sample of the color black

'white''w'

Sample of the color white

Target axes, specified as an Axes object. If you do not specify the axes, then contour3 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.

Example: contour3(Z,'ShowText','on') displays the contour line labels.

Note

The properties listed here are only a subset. For a complete list, see Contour Properties.

Contour line labels, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

  • 'on' — Display the height values along the contour lines.

  • 'off' — Do not label the contour lines.

Contour line width, specified as a positive value in points. One point equals 1/72 inch.

Label spacing along the contour lines, specified as a scalar value in points, where one point is 1/72 inch. Use this property to control the number of contour labels along the contour lines. Smaller values produce more labels.

You must set the ShowText property to 'on' for the LabelSpacing property to have an effect.

If you use the clabel function to display the labels, then the LabelSpacing property has no effect and the plot displays one label per line.

Output Arguments

collapse all

Contour matrix, returned as a two-row matrix of following form.

Z1, x1,1, x1,2, ..., x1,N1, Z2, x2,1, x2,2, ..., x2,N2, Z3, ...
N1, y1,1, y1,2, ..., y1,N1, N2, y2,1, y2,2, ..., y2,N2, N3, ...

The columns of the matrix define the contour lines. Each contour line starts with a column containing Z and N values:

  • Zi — The height of the ith contour line

  • Ni — The number of vertices in the ith contour line

  • (xij, yij) — The coordinates of the vertices for the ith contour line, where j ranges from 1 to Ni

Contour object. Use this object to set properties after displaying the contour plot.

Extended Capabilities

Version History

Introduced before R2006a

See Also

Functions

Properties