# Contour Plots in MATLAB®

How to make Contour Plots plots in MATLAB® with Plotly.

## Contours of a Function

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)

fig2plotly(gcf);


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

fig2plotly(gcf);


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

fig2plotly(gcf);


## 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,'--')

fig2plotly(gcf);


## 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')

fig2plotly(gcf);


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

fig2plotly(gcf);


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

fig2plotly(gcf);


## Contours of Peaks Function

Define Z as a function of two variables. In this case, call the peaks function to create Z. Then display a filled contour plot of Z, letting MATLAB® choose the contour levels.

Z = peaks;
contourf(Z)

fig2plotly(gcf);


## Contours at Ten Levels

Define Z as a function of two variables, X and Y. Then display contours at 10 levels of Z.

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

fig2plotly(gcf);


## Contours at Specific Levels with Labels

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

The white region corresponds to the heights less than 2. The purple region corresponds to heights between 2 and 3. And the yellow region corresponds to heights that are greater than 3.

[X,Y,Z] = peaks(50);
contourf(X,Y,Z,[2 3],'ShowText','on')

fig2plotly(gcf);


## Contours at One Level

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

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

fig2plotly(gcf);


## Dashed Contour Lines

Create a contour plot, and specify the dashed line style.

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

fig2plotly(gcf);


## Custom Line Width

Create a filled contour plot. Make the contour lines thicker by setting the LineWidth property to 3.

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

fig2plotly(gcf);