MATLAB fcontour in MATLAB®
Learn how to make 9 fcontour charts in MATLAB, then publish them to the Web with Plotly.
Plot Contours of Function
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); fcontour(f) fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_0_0_plot_contours_of_function_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_0_0_plot_contours_of_function_montage.png)
Specify Plotting Interval and Plot Piecewise Contour Plot
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
erf(x)+cos(y) -5<x<0
sin(x)+cos(y) 0<x<5
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 fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_1_0_specify_plotting_interval_and_plot_piecewise_contou_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_1_0_specify_plotting_interval_and_plot_piecewise_contou_montage.png)
Change Line Style and Width
Plot the contours of x2-y2 as dashed lines with a line width of 2
.
f = @(x,y) x.^2 - y.^2;
fcontour(f,'--','LineWidth',2)
fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_2_0_change_line_style_and_width_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_2_0_change_line_style_and_width_montage.png)
Plot Multiple Contour Plots
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 fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_3_0_plot_multiple_contour_plots_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_3_0_plot_multiple_contour_plots_montage.png)
Modify Contour Plot After Creation
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)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_4_0_modify_contour_plot_after_creation_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_4_0_modify_contour_plot_after_creation_montage.png)
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]
Show all properties
fc.LineWidth = 1; fc.LineStyle = '--'; fc.LevelList = [1 0.9 0.8 0.2 0.1]; colorbar fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_4_1_modify_contour_plot_after_creation_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_4_1_modify_contour_plot_after_creation_montage.png)
Fill Area Between Contours
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)); fcontour(f,'Fill','on'); fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_5_0_fill_area_between_contours_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_5_0_fill_area_between_contours_montage.png)
If you want interpolated shading instead, use the fsurf
function and set its 'EdgeColor'
option to 'none'
followed by the command view(0,90)
.
Specify Levels for Contour Lines
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])
fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_6_0_specify_levels_for_contour_lines_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_6_0_specify_levels_for_contour_lines_montage.png)
Control Resolution of Contour Lines
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);
tiledlayout(2,1)
nexttile
fcontour(f)
title('Default Mesh Density (71)')
nexttile
fcontour(f,'MeshDensity',200)
title('Custom Mesh Density (200)')
fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_7_0_control_resolution_of_contour_lines_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_7_0_control_resolution_of_contour_lines_montage.png)
Add Title and Axis Labels and Format Ticks
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'}) xlabel('x') ylabel('y') fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_8_0_add_title_and_axis_labels_and_format_ticks_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_8_0_add_title_and_axis_labels_and_format_ticks_montage.png)
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','\pi/2','\pi','3\pi/2','2\pi'}; ax.YTick = ax.YLim(1):pi/2:ax.YLim(2); ax.YTickLabel = {'-2\pi','-3\pi/2','-\pi','-\pi/2','0','\pi/2','\pi','3\pi/2','2\pi'}; fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_8_1_add_title_and_axis_labels_and_format_ticks_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/fcontour/plot_8_1_add_title_and_axis_labels_and_format_ticks_montage.png)
![](/all_static/images/dash.png)