MATLAB mesh in MATLAB®

Learn how to make 4 mesh charts in MATLAB, then publish them to the Web 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('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('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('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')

s = 
  Surface with properties:

       EdgeColor: 'flat'
       LineStyle: '-'
       FaceColor: [1 1 1]
    FaceLighting: 'none'
       FaceAlpha: 0.5000
           XData: [21x21 double]
           YData: [21x21 double]
           ZData: [21x21 double]
           CData: [21x21 double]

  Show 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.
s.FaceColor = 'flat';

fig2plotly('TreatAs', 'mesh')