| Contents | Index |
![]()
To graph selected variables, use the Plot Selector
in the Workspace Browser,
or use the Figure Palette Plot Catalog. Manipulate graphs in plot
edit mode with the Property Editor. For details, see "Plotting
Tools — Interactive Plotting" in the MATLAB Graphics
documentation and "Creating
Graphics from the Workspace Browser" in the MATLAB Desktop
Tools and Development Environment documentation.
contour(Z)
contour(Z,n)
contour(Z,v)
contour(X,Y,Z)
contour(X,Y,Z,n)
contour(X,Y,Z,v)
contour(...,LineSpec)
contour(axes_handle,...)
[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 number of contour lines and the values of the contour lines are chosen automatically based on the minimum and maximum values of Z. The ranges of the x- and y-axis are [1:n] and [1:m], where [m,n] = size(Z).
contour(Z,n) draws a contour plot of matrix Z with n contour levels where n is a scalar.
contour(Z,v) draws a contour plot of matrix Z with contour lines at the data values specified in the monotonically increasing vector v. The number of contour levels is equal to length(v). To draw a single contour of level i, use contour(Z,[i i]). Specifying the vector v sets the LevelListMode to manual to allow user control over contour levels. See contourgroup properties for more information.
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-axis limits. When X and Y are matrices, they must be the same size as Z and must be monotonically increasing.
contour(...,LineSpec) draws the contours using the line type and color specified by LineSpec. contour ignores marker symbols.
contour(axes_handle,...) plots into axes axes_handle instead of gca.
[C,h] = contour(...) returns a contour matrix, C, that contains the x, y coordinates and contour levels for contour lines derived by the low-level contourc function, and a handle, h, to a contourgroup object. The clabel function uses contour matrix C to label the contour lines. ContourMatrix is also a read-only contourgroup property that you can obtain from the returned handle.
Use contourgroup object properties to control the contour plot appearance.
If X or Y is irregularly spaced, contour calculates contours using a regularly spaced contour grid, and then transforms the data to X or Y.
Load the penny data set, which is a 128x128 grid of the surface relief of a U.S. penny. It generates a variable P. To contour this data, pass the matrix into the contour function. You have to flip the graph using flipud to see the actual contour of the image on the penny:
load penny; figure; contour(flipud(P)); axis square;
The figure output is as:

Consider plotting the penny dataset with respect to the diameter distance. Assume the starting point to be 1mm and ending at around 15mm, we need 128 data points on both x and y axes because the dataset is a 128x128 double. To generate 128 elements for both axes, we use linspace function here.
x = linspace(1,15,128);
y = linspace(1,15,128);
figure;
contour(x,y,flipud(P));
axis square;
colormap('Copper');
xlabel('X Distance(in mm)');
ylabel('Y Distance(in mm)');

To plot a contour with more levels,
figure;
contour(x,y,flipud(P),50);
axis square;
colormap('Copper');The figure looks like this.

Create a contour plot of the function:
z = xe(–x2 – y2)
over the range –2 ≤ x ≤ 2, –2 ≤ y ≤ 3.
Evaluate the function to create matrix, Z. Use the meshgrid function to generate the values used to evaluate the function within the specified range:
[X,Y] = meshgrid(-2:.2:2,-2:.2:3); Z = X.*exp(-X.^2-Y.^2);
Generate the contour plot of Z:
Display contour labels by setting the ShowText property to on.
Label every other contour line by setting the TextStep property to twice the contour interval (that is, two times the LevelStep property).
Use a smoothly varying colormap.
[C,h] = contour(X,Y,Z); set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2) colormap cool

Use interp2 to smooth contour lines. Also set the contour label text BackgroundColor to a light yellow and the EdgeColor to light gray.
Z = peaks;
[C,h] = contour(interp2(Z,4));
text_handle = clabel(C,h);
set(text_handle,'BackgroundColor',[1 1 .6],...
'Edgecolor',[.7 .7 .7])

For more examples using contour, see Contour Plots.
The contour function cannot determine if there are discontinuities in the input data. You can add NaNs to the data to prevent drawing the contour lines in those regions. For example, add NaN to the y data where y == 0 and x < 0:
[x,y] = meshgrid(-2.05:.1:2.05,[-2.05:.1:-0.05,0,0.05:.1:2.05]);
y(y==0 & x<0) = NaN; % Explicitly add NaNs to data
contour(x,y,atan2(y,x))clabel | contour3 | contourc | contourf | contourgroup properties | quiver | text properties

Explore how to use MATLAB to make advancements in engineering and science.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |