Contour Plots (MATLAB)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CONTOUR PLOTS

 contour(Z) creates a 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.

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

Create matrices X and Y, that define a grid in the x-y plane. Define matrix Z as
the heights above that grid. Then plot the contours of Z.

x = linspace(-2*pi,2*pi);

y = linspace(0,4*pi);

[X,Y] = meshgrid(x,y);

Z = sin(X)+cos(Y);

contour(X,Y,Z)

Contours at Twenty Levels

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

[X,Y,Z] = peaks;

contour(X,Y,Z,20)

Contours at One Level

Display the contours of the peaks function at Z = 1.

[X,Y,Z] = peaks;
v = [1,1];
contour(X,Y,Z,v)
Dashed Contour Lines
Create a contour plot of the peaks function, and specify the dashed line style.

[X,Y,Z] = peaks;
contour(X,Y,Z,'--')

Contours with Labels


Define Z as a function of two variables, X and Y. Then create a contour plot of
that function, and display the 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);
contour(X,Y,Z,'ShowText','on')

Custom Line Width


Create a contour plot of the peaks function. Make the contour lines thicker by
setting the LineWidth property to 3.

Z = peaks;
[M,c] = contour(Z);
c.LineWidth = 3;

Contours Over Discontinuous Surface

Insert NaN values wherever there are discontinuities on a surface. The contour
function does not draw contour lines in those regions.

Define matrix Z as a sampling of the peaks function. Replace all values in column
26 with NaN values. Then plot the contours of the modified Z matrix.

Z = peaks;
Z(:,26) = NaN;
contour(Z)
contourf(Z) creates a filled 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.

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

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.

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

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);

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)
Now specify 50 contour levels, and display the results within the x and y limits
used to calculate Z.

contour3(X,Y,Z,50)
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 Show Text 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')

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]);

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;

You might also like