3D Mesh Plots in MATLAB®

How to make 3D Mesh Plots plots in MATLAB® with Plotly.


Create Mesh Plot

Create three matrices of the same size. Then plot them as a mesh plot. The plot uses Z for both height and color.

[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
mesh(X,Y,Z);

fig2plotly(gcf, 'TreatAs', 'mesh');

Specify Colormap Colors for Mesh Plot

Specify the colors for a mesh plot by including a fourth matrix input, C. The mesh 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(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
C = X.*Y;
mesh(X,Y,Z,C);
colorbar;

fig2plotly(gcf, 'TreatAs', 'mesh');

Specify True Colors for Mesh Plot

Specify the colors for a mesh plot by including a fourth matrix input, CO. The mesh plot uses Z for height and CO for color. Specify the colors using truecolor, which uses triplets of numbers to stand for all possible colors. When you use truecolor, if Z is m-by-n, then CO is m-by-n-by-3. The first page of the array indicates the red component for each color, the second page indicates the green component, and the third page indicates the blue component.

[X,Y,Z] = peaks(25);
CO(:,:,1) = zeros(25); % red
CO(:,:,2) = ones(25).*linspace(0.5,0.6,25); % green
CO(:,:,3) = ones(25).*linspace(0,1,25); % blue
mesh(X,Y,Z,CO);

fig2plotly(gcf, 'TreatAs', 'mesh');

Modify Mesh Plot Appearance

Create a semitransparent mesh surface by specifying the FaceAlpha name-value pair with 0.5 as the value. To allow further modifications, assign the surface object to the variable s.

[X,Y] = meshgrid(-5:.5:5);
Z = Y.*sin(X) - X.*cos(Y);
s = mesh(X,Y,Z,'FaceAlpha','0.5')

fig2plotly(gcf, 'TreatAs', 'mesh');
s = 

  all properties

Use s to access and modify properties of the mesh plot after it is created. For example, add color to the face of the mesh plot by setting the FaceColor property.

[X,Y] = meshgrid(-5:.5:5);
Z = Y.*sin(X) - X.*cos(Y);
s = mesh(X,Y,Z,'FaceAlpha','0.5')

s.FaceColor = 'flat';

fig2plotly(gcf, 'TreatAs', 'mesh');
s = 

  all properties