March 10

# Contour in MATLAB: Plot Overview

Abdul Mannan

## What is a Contour in MATLAB?

Contour map in MATLAB is a graphical representation of a 3D surface against constant z-axis to show it in a 2D format.

When we try to plot 3D map in MATLAB, we have different options to plot. One of them is contour map which shows a 3D plot in 2D format having constant z-axis. A curved line of the same color shows the same elevation point. For example, if we want to plot a 2D diagram of a 3D picture shown in fig. 1, we’ll draw curved lines showing the height and steepness on the map.

Fig. 2 shows contour map of fig. 1. We see that each circular line is at the same elevation point and the steepness is shown by saturated area. Top area is a little bit flat that’s why it is not saturated.

## Plotting Contour in MATLAB with Eight Different Commands

We will introduce eight basic commands for contour map plot:

1. CONTOUR (produces plane contour map).
2. CONTOUR3 (produces 3D contour map).
3. CONTOURF (produces contour map with lines filled).
4. EZCONTOUR
5. EZCONTOURF
6. EZMESHC
7. SURFC
8. EZSURF

### ‘CONTOUR’ Command

For producing plane contour maps, we use ‘contour’ command.

Fig. 4 is showing the output of MATLAB code.

Normally contour map will give you ten lines. Each color line is at the same height level. If we want more lines, we will use the command:

Contour(x,y,z,25) instead of Contour(x,y,z). This will give us 25 line contour map.

If we want to label the map, we use command ‘clabel’. Labeled height levels are shown in this figure.

[c,h] = contour(x,y,z)

Clabel(c,h)

### ‘CONTOUR3’ Command

Using the command contour3(x,y,z), we get 3D contour plot of a function.

If we increase number of lines to 25:

Contour(x,y,z,25)

### CONTOURF Command

It produces the contour map with the lines filled.

Contourf(x,y,z) command is used.

### EZCONTOUR Command

Write a function in the following way:

Ezcontour(‘x^2-y^2’)

Output will be:

### EZCONTOURF Command

Ezcontourf(‘x^2-y^2’) command produces contour with lines filled.

### SURFC Command

It is used to produce surface plot for a function. It uses the same syntax for producing the mesh surface and contour map simultaneously.

### EZSURFC Command

Ezsurfc(‘x^2-y^2’) command is used to plot the same thing without declaring mesh surface equations and using the previous syntax.

### EZMESHC Command

Ezmeshc(‘x^2-y^2’)  shows both surface and the contour lines on the same set of axis.

## Conclusion

When we plot a 3D surface in MATLAB, we can plot its contour in 2D format with one constant axis. Contour of any function can be plotted in MATLAB and we can see the different height levels represented with different line colors and labeled using ‘clabel’ command. Also we can plot the surface and the contour simultaneously.

## References

www.mathworks.com

MATLAB software

Abdul Mannan is an Electrical Power Engineer with specialization in High Voltage. He's the founder and former president at Youth Entrepreneurship Society (YES), University of Engineering & Technology Taxila Campus. He is the leading contributor at "Right to Write". Connect with him about Entrepreneurship, startup ideas, creative writing, business strategies via linked in.

Abdul Mannan