This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Contour plot of matrix


[C,h] = contour(...)


A contour plot displays isolines of matrix Z. Label the contour lines using clabel.

contour(Z) draws a contour plot of matrix Z, where Z is interpreted as heights with respect to the x-y plane. Z must be at least a 2-by-2 matrix that contains at least two different values. The x values correspond to the column indices of Z and the y values correspond to the row indices of Z. The contour levels are chosen automatically.

contour(Z,n) draws a contour plot of matrix Z with n contour levels where n is a scalar. The contour levels are chosen automatically.

contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v. To display a single contour line at a particular value, define v as a two-element vector with both elements equal to the desired contour level. For example, to draw contour lines at level k, use contour(Z,[k k]). Specifying the vector v sets the LevelListMode property to manual.

contour(X,Y,Z), contour(X,Y,Z,n), and contour(X,Y,Z,v) draw contour plots of Z using X and Y to determine the x and y values.

  • If X and Y are vectors, then length(X) must equal size(Z,2) and length(Y) must equal size(Z,1). The vectors must be strictly increasing or strictly decreasing and cannot contain any repeated values.

  • If X and Y are matrices, then their sizes must equal the size of Z. Typically, you should set X and Y so that the columns are strictly increasing or strictly decreasing and the rows are uniform (or the rows are strictly increasing or strictly decreasing and the columns are uniform).

If X or Y is irregularly spaced, then contour calculates contours using a regularly spaced contour grid, and then transforms the data to X or Y.

contour(...,LineSpec) draws the contours using the line type and color specified by LineSpec. contour ignores marker symbols.

contour(...,Name,Value) specifies contour properties using one or more property name, property value pairs. Name is the property name and must appear inside single quotes (''). Value is the corresponding value. For example, 'LineWidth',2 sets the contour line width to 2. For a list of contour property names and values, see Contour Properties.

contour(ax,...) plots into the axes specified by ax instead of the current axes (gca).

[C,h] = contour(...) returns the contour matrix C containing the data that defines the contour lines, and the Contour object h. The ContourMatrix property for the Contour object also contains the contour matrix. The clabel function uses the contour matrix to label the contour lines.

Use Contour object properties to control the contour plot appearance. For a list, see Contour Properties.


collapse all

Use the meshgrid function to generate matrices X and Y. Create a third matrix, Z, and plot its contours.

x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);


Store the data from the peaks function in matrices X, Y, and Z. Plot 20 contours of the data in Z.

[X,Y,Z] = peaks;

Set up matrices X, Y, and Z. Create a contour plot and display the contour labels by setting the ShowText property to on.

x = -2:0.2:2;
y = -2:0.2:3;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);


Create a contour plot of the peaks function and display only one contour level at Z = 1.

x = -3:0.125:3;
y = -3:0.125:3;
[X,Y] = meshgrid(x,y);
Z = peaks(X,Y);
v = [1,1];



  • The contour function cannot determine if there are discontinuities in the input data. Add NaN values to the data to prevent drawing the contour lines in those regions.

Introduced before R2006a

Was this topic helpful?