How to Plot 3D Contour in MATLAB

Ammar Ali Feb 15, 2024
How to Plot 3D Contour in MATLAB

This tutorial will discuss creating a 3D contour plot using the contour3() function in MATLAB.

Create a 3D Contour Plot Using the contour3() Function in MATLAB

We can use MATLAB’s built-in function contour3() to create a 3D contour plot. A contour plot is a plot of isolines with different colors according to values.

The color given to the line depends on its value. The colder color corresponds to low values, and the hotter color corresponds to high values.

For example, let’s plot a 3D contour plot of a sphere using the sphere() and the contour3() function. See the code below.

[x,y,z] = sphere(100);
contour3(x,y,z);

Output:

3D contour plot of a sphere

We can also set some properties of the contour plot like the line levels, line specifications, line style, line color, line labels, line width, and label spacing.

We can set the value of levels to any scalar or vector value. We can also set the line specification like the color and line style simultaneously using the LineSpec property.

By default, the show text is set to off, but we can turn it on and show any text on the plot. By default, the line width is 0.5, but we can change to any positive scaler value using the LineWidth property.

The default value of the label spacing is 144, but we can set it to any scalar using the LabelSpacing property. We can give a title to the plot, and we can also set the labels of the plot.

For example, let’s change some properties of the above graph. See the code below.

[x,y,z] = sphere(100);
contour3(x,y,z,20,'Color','red','LineWidth',1.5,'LineStyle','-.')

Output:

changing properties of 3D contour plot

We can also create an object of the contour3() function, which we can use to change the properties of the plot after it is created.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Plot

Related Article - MATLAB 3D Plot