Contour Plots (MATLAB)
Contour Plots (MATLAB)
Contour Plots (MATLAB)
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)
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)
[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,'--')
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')
Z = peaks;
[M,c] = contour(Z);
c.LineWidth = 3;
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.
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.
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);
[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]);
[X,Y] = meshgrid(-2:0.0125:2);
Z = X.*exp(-X.^2-Y.^2);
[M,c] = contour3(X,Y,Z,30);
c.LineWidth = 3;