Main Content


  • Plot contours



fcontour(f) plots the contour lines of the function z = f(x,y) for constant levels of z over the default interval [-5 5] for x and y.


fcontour(f,xyinterval) plots over the specified interval. To use the same interval for both x and y, specify xyinterval as a two-element vector of the form [min max]. To use different intervals, specify a four-element vector of the form [xmin xmax ymin ymax].

fcontour(___,LineSpec) sets the line style and color for the contour lines. For example, '-r' specifies red lines. Use this option after any of the previous input argument combinations.


fcontour(___,Name,Value) specifies line properties using one or more name-value pair arguments.

fcontour(ax,___) plots into the axes specified by ax instead of the current axes.


fc = fcontour(___) returns a FunctionContour object. Use fc to query and modify properties of a specific FunctionContour object. For a list of properties, see FunctionContour Properties.


collapse all

Plot the contours of f(x,y)=sin(x)+cos(y) over the default interval of -5<x<5 and -5<y<5.

f = @(x,y) sin(x) + cos(y);

Specify the plotting interval as the second argument of fcontour. When you plot multiple inputs over different intervals in the same axes, the axis limits adjust to display all the data. This behavior lets you plot piecewise inputs.

Plot the piecewise input


over -5<y<5.

fcontour(@(x,y) erf(x) + cos(y),[-5 0 -5 5])
hold on
fcontour(@(x,y) sin(x) + cos(y),[0 5 -5 5])
hold off
grid on

Plot the contours of x2-y2 as dashed lines with a line width of 2.

f = @(x,y) x.^2 - y.^2;

Plot sin(x)+cos(y) and x-y on the same axes by using hold on.

fcontour(@(x,y) sin(x)+cos(y))
hold on
fcontour(@(x,y) x-y)
hold off

Plot the contours of e-(x/3)2-(y/3)2+e-(x+2)2-(y+2)2. Assign the function contour object to a variable.

f = @(x,y) exp(-(x/3).^2-(y/3).^2) + exp(-(x+2).^2-(y+2).^2);
fc = fcontour(f)

fc = 
  FunctionContour with properties:

     Function: @(x,y)exp(-(x/3).^2-(y/3).^2)+exp(-(x+2).^2-(y+2).^2)
    LineColor: 'flat'
    LineStyle: '-'
    LineWidth: 0.5000
         Fill: off
    LevelList: [0.2000 0.4000 0.6000 0.8000 1 1.2000 1.4000]

  Use GET to show all properties

Change the line width to 1 and the line style to a dashed line by using dot notation to set properties of the function contour object. Show contours close to 0 and 1 by setting the LevelList property. Add a colorbar.

fc.LineWidth = 1;
fc.LineStyle = '--';
fc.LevelList = [1 0.9 0.8 0.2 0.1];

Create a plot that looks like a sunset by filling the area between the contours of


f = @(x,y) erf((y+2).^3) - exp(-0.65*((x-2).^2+(y-2).^2));

If you want interpolated shading instead, use the fsurf function and set its 'EdgeColor' option to 'none' followed by the command view(0,90).

Set the values at which fcontour draws contours by using the 'LevelList' option.

f = @(x,y) sin(x) + cos(y);
fcontour(f,'LevelList',[-1 0 1])

Control the resolution of contour lines by using the 'MeshDensity' option. Increasing 'MeshDensity' can make smoother, more accurate plots, while decreasing it can increase plotting speed.

Create two plots in a 2-by-1 tiled chart layout. In the first plot, display the contours of sin(x)sin(y). The corners of the squares do not meet. To fix this issue, increase 'MeshDensity' to 200 in the second plot. The corners now meet, showing that by increasing 'MeshDensity' you increase the resolution.

f = @(x,y) sin(x).*sin(y);
title('Default Mesh Density (71)')

title('Custom Mesh Density (200)')

Plot xsin(y)-ycos(x). Display the grid lines, add a title, and add axis labels.

fcontour(@(x,y) x.*sin(y) - y.*cos(x), [-2*pi 2*pi], 'LineWidth', 2);
grid on
title({'xsin(y) - ycos(x)','-2\pi < x < 2\pi and -2\pi < y < 2\pi'})

Set the x-axis tick values and associated labels by setting the XTickLabel and XTick properties of the axes object. Access the axes object using gca. Similarly, set the y-axis tick values and associated labels.

ax = gca;
ax.XTick = ax.XLim(1):pi/2:ax.XLim(2);
ax.XTickLabel = {'-2\pi','-3\pi/2','-\pi','-\pi/2','0',...

ax.YTick = ax.YLim(1):pi/2:ax.YLim(2);
ax.YTickLabel = {'-2\pi','-3\pi/2','-\pi','-\pi/2','0',...

Input Arguments

collapse all

Function to plot, specified as a function handle to a named or anonymous function.

Specify a function of the form z = f(x,y). The function must accept two matrix input arguments and return a matrix output argument of the same size. Use array operators instead of matrix operators for the best performance. For example, use .* (times) instead of * (mtimes).

Example: f = @(x,y) sin(x) + cos(y);

Plotting interval for x and y, specified in one of these forms:

  • Vector of form [min max] — Use the interval [min max] for both x and y.

  • Vector of form [xmin xmax ymin ymax] — Use the interval [xmin xmax] for x and [ymin ymax] for y.

Axes object. If you do not specify an axes object, then the fcontour uses the current axes.

Line style and color, specified as a character vector or string containing a line style specifier, a color specifier, or both.

Example: '--r' specifies red dashed lines

These two tables list the line style and color options.

Line Style SpecifierDescription
-Solid line (default)
--Dashed line
:Dotted line
-.Dash-dot line
Color SpecifierDescription

















Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'MeshDensity',30

The properties listed here are only a subset. For a full list, see FunctionContour Properties.

Number of evaluation points per direction, specified as a number. The default is 71. Because fcontour uses adaptive evaluation, the actual number of evaluation points is greater.

Example: 30

Fill between contour lines, specified as 'on' or 'off', or as numeric or logical 1 (true) or 0 (false). A value of 'on' is equivalent to true, and 'off' is equivalent to false. Thus, you can use the value of this property as a logical value. The value is stored as an on/off logical value of type matlab.lang.OnOffSwitchState.

  • A value of 'on' fill the spaces between contour lines with color.

  • A value of 'off' leaves the spaces between the contour lines unfilled.

Contour levels, specified as a vector of z values. By default, the fcontour function chooses values that span the range of values in the ZData property.

Setting this property sets the associated mode property to manual.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Spacing between contour lines, specified as a scalar numeric value. For example, specify a value of 2 to draw contour lines at increments of 2. By default, LevelStep is determined by using the ZData values.

Setting this property sets the associated mode property to 'manual'.

Example: 3.4

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Color of contour lines, specified as 'flat', an RGB triplet, a hexadecimal color code, a color name, or a short name. To use a different color for each contour line, specify 'flat'. The color is determined by the contour value of the line, the colormap, and the scaling of data values into the colormap. For more information on color scaling, see Control Colormap Limits.

To use the same color for all the contour lines, specify an RGB triplet, a hexadecimal color code, a color name, or a short name.

For a custom color, specify an RGB triplet or a hexadecimal color code.

  • An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1], for example, [0.4 0.6 0.7].

  • A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Therefore, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

"none"Not applicableNot applicableNot applicableNo color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]"#0072BD"

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980]"#D95319"

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250]"#EDB120"

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560]"#7E2F8E"

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880]"#77AC30"

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330]"#4DBEEE"

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840]"#A2142F"

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

Line width, specified as a positive value in points, where 1 point = 1/72 of an inch. If the line has markers, then the line width also affects the marker edges.

The line width cannot be thinner than the width of a pixel. If you set the line width to a value that is less than the width of a pixel on your system, the line displays as one pixel wide.

Output Arguments

collapse all

One or more FunctionContour objects, returned as a scalar or a vector. You can use these objects to query and modify the properties of a specific contour plot. For a list of properties, see FunctionContour Properties.

Version History

Introduced in R2016a

See Also