MATLAB surfc in MATLAB®
Learn how to make 4 surfc charts in MATLAB, then publish them to the Web with Plotly.
Display Contour Plot Under Surface Plot
Create three matrices of the same size. Then plot them as a surface and display a contour plot under the surface plot. The surface uses Z
for both height and color.
[X,Y] = meshgrid(1:0.5:10,1:20); Z = sin(X) + cos(Y); surfc(X,Y,Z) fig2plotly('TreatAs', 'surfc')
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_0_0_display_contour_plot_under_surface_plot_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_0_0_display_contour_plot_under_surface_plot_montage.png)
Specify Colormap Colors for Surface and Contour Plot
Specify the colors for a surface and a contour plot by including a fourth matrix input, C
. The surface plot uses Z
for height and C
for color. Specify the colors using a colormap, which uses single numbers to stand for colors on a spectrum. When you use a colormap, C
is the same size as Z
. Add a color bar to the graph to show how the data values in C
correspond to the colors in the colormap.
[X,Y] = meshgrid(-3:.125:3); Z = peaks(X,Y); C = X.*Y; surfc(X,Y,Z,C) colorbar fig2plotly('TreatAs', 'surfc')
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_1_0_specify_colormap_colors_for_surface_and_contour_plo_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_1_0_specify_colormap_colors_for_surface_and_contour_plo_montage.png)
Modify Appearance of Surface and Contour Plots
Create a blue surface plot with a contour plot underneath it by specifying the FaceColor
name-value pair with 'b'
as the value. To allow further modifications, assign the graphics array containing the surface and contour objects to the variable sc
.
[X,Y] = meshgrid(-5:.5:5); Z = Y.sin(X) - X.cos(Y); sc = surfc(X,Y,Z,'FaceColor','b');
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_2_0_modify_appearance_of_surface_and_contour_plots_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_2_0_modify_appearance_of_surface_and_contour_plots_montage.png)
Index into sc
to access and modify properties of the surface and contour plots after they are created. The surface plot is accessible as sc(1)
and the contour plot as sc(2)
. For example, change the edge colors of the two plots by setting the EdgeColor
properties.
sc(1).EdgeColor = 'r'; sc(2).EdgeColor = 'b'; fig2plotly('TreatAs', 'surfc')
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_2_1_modify_appearance_of_surface_and_contour_plots_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_2_1_modify_appearance_of_surface_and_contour_plots_montage.png)
Change Location of Contour Lines
The contour lines appear at the minimum z-level by default, but you can change the location by setting the ZLocation
property.
Display the peaks
data set as a surface plot with the contours at the minimum z-level. Specify a return argument when you call the surfc
function so that you can access the Contour
object.
Z = peaks; sc = surfc(Z);
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_3_0_change_location_of_contour_lines_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_3_0_change_location_of_contour_lines_montage.png)
Get the current axes and extend the upper limit of the z-axis to 15
. Then move the contours to the maximum z-level.
ax = gca;
ax.ZLim(2) = 15;
sc(2).ZLocation = 'zmax';
fig2plotly('TreatAs', 'surfc')
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_3_1_change_location_of_contour_lines_ssim_map.png)
![](https://raw.githubusercontent.com/plotly/ssim_baselines/main/out_matlab/matlab/code-examples/surface-and-mesh-plots/surfc/plot_3_1_change_location_of_contour_lines_montage.png)