MATLAB contourf in MATLAB®
Learn how to make 7 contourf charts in MATLAB, then publish them to the Web with Plotly.
Contours of Peaks Function
Define Z
as a function of two variables. In this case, call the peaks
function to create Z
. Then display a filled contour plot of Z
, letting MATLAB® choose the contour levels.
Z = peaks; contourf(Z) fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_0_0_contours_of_peaks_function_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_0_0_contours_of_peaks_function_montage.png)
Contours at Ten Levels
Define Z
as a function of two variables, X
and Y
. Then display contours at 10 levels of Z
.
x = linspace(-2*pi,2*pi); y = linspace(0,4*pi); [X,Y] = meshgrid(x,y); Z = sin(X) + cos(Y); contourf(X,Y,Z,10) fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_1_0_contours_at_ten_levels_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_1_0_contours_at_ten_levels_montage.png)
Contours at Specific Levels with Labels
Define Z
as a function of X
and Y
. In this case, call the peaks
function to create X
, Y
, and Z
. Then display contours at levels 2
and 3
.
The white region corresponds to the heights less than 2
. The purple region corresponds to heights between 2
and 3
. And the yellow region corresponds to heights that are greater than 3
.
[X,Y,Z] = peaks(50); contourf(X,Y,Z,[2 3],'ShowText','on') fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_2_0_contours_at_specific_levels_with_labels_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_2_0_contours_at_specific_levels_with_labels_montage.png)
Contours at One Level
Define Z
as a function of X
and Y
. In this case, call the peaks
function to create X
, Y
, and Z
. Then display contours at Z = 2
.
[X,Y,Z] = peaks; contourf(X,Y,Z,[2 2]) fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_3_0_contours_at_one_level_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_3_0_contours_at_one_level_montage.png)
Dashed Contour Lines
Create a contour plot, and specify the dashed line style.
[X,Y,Z] = peaks; contourf(X,Y,Z,'--') fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_4_0_dashed_contour_lines_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_4_0_dashed_contour_lines_montage.png)
Custom Line Width
Create a filled contour plot. Make the contour lines thicker by setting the LineWidth
property to 3
.
Z = peaks; [M,c] = contourf(Z); c.LineWidth = 3; fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_5_0_custom_line_width_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_5_0_custom_line_width_montage.png)
Contours Over Discontinuous Surface
Insert NaN
values wherever there are discontinuities on a surface. The contourf
function does not draw contour lines in those regions.
Define matrix Z
as a sampling of the peaks
function. Replace all values in column 26
with NaN
values. Then plot the contours of the modified Z
matrix.
Z = peaks; Z(:,26) = NaN; contourf(Z) fig2plotly()
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_6_0_contours_over_discontinuous_surface_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/contour-plots/contourf/plot_6_0_contours_over_discontinuous_surface_montage.png)