Skip to main content
v2026.11,986 entries · CC-BY 4.0

Contour Plot in MATLAB: contour, contourf and Levels

Step-by-step MATLAB contour plots: contour vs contourf, setting levels, clabel and colorbar, the contour matrix, and troubleshooting.

Written and maintained by CASRAI Editorial Board

Published

On this page

To plot a contour in MATLAB, build a grid with meshgrid, compute the heights Z, and call contour(X,Y,Z) for lines or contourf(X,Y,Z) for filled bands. This guide covers the documented syntaxes, how to set contour levels, how to label and colour a contour plot, how to read the contour matrix MATLAB returns, and what to check when the plot looks wrong. It is written from the MathWorks function reference pages for contour, contourf, clabel and colorbar, read on 2026-11-08.

What a contour plot is used for in research

A contour plot shows a quantity that depends on two variables as lines of constant value (level curves) on a flat plane. Researchers use it for response surfaces, spatial fields, density estimates and any matrix of values where the shape of the surface matters more than individual points. If you want the same surface drawn in three dimensions, see plotting in 3D in MATLAB; contour3 is the 3-D contour function named in the MathWorks “See Also” list for contour.

Access: where the functions come from

contour, contourf, clabel and colorbar are documented in the core MATLAB function reference at mathworks.com/help/matlab, so they need no add-on toolbox. Get MATLAB from MathWorks directly or through your institution’s software office; this page does not cover licence terms. Free alternatives are listed further down.

Step-by-step: plot a contour in MATLAB

1. Prepare Z (and optionally X and Y)

The documentation requires Z to have at least two rows and two columns and at least two different values. X and Y can be vectors (the defaults are 1:n and 1:m) or matrices the same size as Z; to build matching matrices use meshgrid (the Python equivalent is covered in NumPy meshgrid).

[X,Y] = meshgrid(-2:0.2:2);
Z = X .* exp(-X.^2 - Y.^2);

2. Draw the contour lines (matlab contour plot)

contour(X,Y,Z)

With only contour(Z), MATLAB uses column and row indices as the x and y coordinates.

3. Set the contour levels (contour interval)

  • A scalar n asks for n automatically chosen levels: contour(X,Y,Z,10).
  • A vector of increasing heights draws exactly those levels: contour(X,Y,Z,[-0.2 0 0.2]).
  • To draw a single level at height k, pass a two-element row vector [k k]: contour(X,Y,Z,[0.1 0.1]). A lone scalar would be read as a count of levels, not a height.

4. Style the lines and add labels

A LineSpec such as "--g" sets line style and colour (marker symbols are ignored). Name-value arguments include ShowText ("on" labels the lines; default "off"), LineWidth (default 0.5) and LabelSpacing (default 144, used only with ShowText on).

contour(X,Y,Z,10,"--g",ShowText="on",LineWidth=1)

For more control, return the matrix and object and call clabel:

[M,c] = contour(X,Y,Z);
clabel(M,c)
c.LineWidth = 3;

clabel(C,h) labels the current contour plot with rotated text in each line; clabel(C,h,v) labels only the levels in vector v; clabel(C,h,"manual") lets you click to place labels and press Return to finish.

5. Make a filled contour plot (contourf)

contourf(X,Y,Z)
colorbar

contourf accepts the same syntax set as contour and fills between levels using the current colormap: the first colour fills the space between the lowest level and the level above it. colorbar displays the current colormap and indicates how data values map onto it; colorbar(location) such as colorbar("northoutside") moves it (not all chart types support changing the location). To change the colours, see MATLAB colormaps.

Which function: contour, contourf or contour3

Function Output Documented calls
contour Contour lines of Z contour(Z), contour(X,Y,Z), contour(___,levels), contour(___,LineSpec), contour(___,Name=Value), contour(ax,___), M = contour(___), [M,c] = contour(___)
contourf Filled contour plot Same syntax set as contour
contour3 Listed under See Also for both pages; see its own reference page for syntax Not covered here

Output and how to read it

M = contour(___) returns a two-row contour matrix: for each line, a column gives the height Zi and the vertex count Ni, followed by the x,y vertices of that line. [M,c] = contour(___) also returns a Contour object c, whose properties you can set after plotting (for example c.LineWidth = 3). Pass M (or [] if you do not have it) and c to clabel. Reading the plot itself: closely spaced lines mean Z changes quickly there; widely spaced lines mean a flat region.

Troubleshooting

  • Error or empty plot: check that Z has at least two rows, two columns and two different values, as required by the documentation.
  • X and Y mismatch: X and Y must be vectors or matrices matching Z’s size; build them with meshgrid.
  • One level only: use [k k], not k.
  • White area in contourf: the region between the lowest level and the smallest Z value is white, per the contourf page. Add a lower level in the levels argument.
  • Scattered (non-gridded) data: contour expects Z on a grid, so interpolate scattered points onto a meshgrid first (for example with griddata), then contour the gridded result.

Free alternatives

Tool Equivalent Guide
Python with Matplotlib contour and contourf Contour plots in Matplotlib

FAQ

How do I plot contours in MATLAB?

Create X and Y with meshgrid, compute Z, then call contour(X,Y,Z). Use contourf for filled bands.

How do I set the contour levels or interval?

Pass a number of levels (contour(X,Y,Z,10)) or a vector of heights (contour(X,Y,Z,[-0.2 0 0.2])). For a single level use [k k].

How do I label contour lines?

Set ShowText="on", or call clabel(M,c) after [M,c] = contour(___).

How do I add a legend or colour scale to contourf?

Use colorbar, which shows the current colormap and how data values map to it.

Can I contour scattered data?

Not directly. Interpolate onto a regular grid first, then contour the gridded Z.

Sources (read 2026-11-08)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Contour Plot in MATLAB: contour, contourf and Levels

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →