Skip to main content
Library homepage
 
Engineering LibreTexts

15.2: Plot 3D Surface and Contour Plots

  • Page ID
    85207
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    By Carey A. Smith

    meshgrid() function

    Surface plots need 2-dimensional grids of x and y coordinates. These are made from 1-dimensional vectors of x and y coordinates using the meshgrid() function. The syntax of meshgrid() is:

    [x2D, y2D] = meshgrid(x1D,y1D);

    Example \(\PageIndex{1}\) Simple meshgrid() Example

    x1D = [0, 1, 2 ,3];
    y1D = [0, 2, 4];
    [x2D, y2D] = meshgrid(x1D,y1D)

    Solution

    x2D =
    0 1 2 3
    0 1 2 3
    0 1 2 3
    y2D =
    0 0 0 0
    2 2 2 2
    4 4 4 4

    As can be seen:

    The x coordinates are repeated in all the rows.

    The x coordinates are repeated in all the columns.

    Watch this video about meshgrid. Note that the presenter uses lower case x & y for 1-dimensional values and upper case X & Y for 1-dimensional values. It is not good practice to use variables names that only differ by lower and upper case. Better is to use different name, like x1D, y1D and x2D, y2D. The important part is 0 to 4:20 minutes.

    .

    mesh() 3D Plot

    The mesh() function creates a wire-frame 3-dimensional plot.

    Example \(\PageIndex{2}\) mesh() 3D Plot

    %% Create a 3D graph of r^2 = x^2 + y^2 over a grid of points
    % 1. Set the x and y axis 1D vectors that define this rectangular grid:
    x1D = -3 : 0.5 :3; % 1x13 vector
    y1D = -2.5: 0.5 :2.5; % 1x11 vector

    %% 2. Matlab's meshgrid() function uses these 1D vectors
    % to create 2D arrays of x & y at each point of the rectangular grid.
    [x2D, y2D] = meshgrid(x1D,y1D);

    %% 3. Compute z(x,y) at each point in the grid.
    z = x2D.^2 + 1*x2D + y2D.^2 + 2*y2D;

    %% mesh(x,y,z) creates a 3D wire frame plot.
    figure;
    mesh(x2D, y2D, z);
    title('mesh(x2D, y2D, Z)');
    colormap jet % This selects a colormap
    colorbar; % This displays the color scale
    xlabel('x') % This identifies x-axis
    ylabel('y')
    zlabel('z')

    Solutionmesh_3D_example.png

    The resulting plot is shown here.

    Figure \(\PageIndex{1}\): mesh() 3D plot

    .

    surf() 3D plot

    The surf() function also creates a continous surface 3-dimensional plot. Its usage is the same as the mesh() function.

    Example \(\PageIndex{3}\) surf() 3D Plot

    format short g; format compact; clear all; close all; clc;

    %% Create a 3D graph of r^2 = x^2 + y^2 over a grid of points
    % 1. Set the x and y axis 1D vectors that define this rectangular grid:
    x1D = -3 : 0.5 :3; % 1x13 vector
    y1D = -2.5: 0.5 :2.5; % 1x11 vector

    %% 2. Matlab's meshgrid() function uses these 1D vectors
    % to create 2D arrays of x & y at each point of the rectangular grid.
    [x2D, y2D] = meshgrid(x1D,y1D);

    %% 3. Compute z(x,y) at each point in the grid.
    z = x2D.^2 + 1*x2D + y2D.^2 + 2*y2D;

    %% surf(x,y,z) creates a 3D continous surface plot.
    figure;
    surf(x2D, y2D, z);
    title('mesh(x2D, y2D, Z)');
    colormap hot % This selects a colormap
    colorbar; % This displays the color scale
    xlabel('x') % This identifies x-axis
    ylabel('y')
    zlabel('z')
    surf_3D_example.png

    Solution

    The resulting plot is shown here.

    Figure \(\PageIndex{2}\): surf() 3D plot

    See this attached file for images of the colormap options.: colormap_options.pdf

    .

    contour() Plot

    Contour plots are like topographical maps. They display 3D data in a 2D format. The basic syntax is:

    contour(x2D, y2D, z);

    This can be improved by specifying the contour levels and showing their values:

    contour(x2D, y2D, z, levels, 'ShowText','on')); % This creates specified contours and shows their values.

    Example \(\PageIndex{4}\) contour() Plot

    format short g; format compact; clear all; close all; clc;

    %% Create a 3D graph of r^2 = x^2 + y^2 over a grid of points
    % 1. Set the x and y axis 1D vectors that define this rectangular grid:
    x1D = -3 : 0.5 :3; % 1x13 vector
    y1D = -2.5: 0.5 :2.5; % 1x11 vector

    %% 2. Matlab's meshgrid() function uses these 1D vectors
    % to create 2D arrays of x & y at each point of the rectangular grid.
    [x2D, y2D] = meshgrid(x1D,y1D);

    %% 3. Compute z(x,y) at each point in the grid.
    z = x2D.^2 + 1*x2D + y2D.^2 + 2*y2D;

    %% contour(x,y,z) creates a continous surface plot.
    figure;
    levels = [-4: 2: 24]; % Specify the contours
    contour(x2D, y2D, z, levels, 'ShowText','on');
    title('contour(x2D, y2D, Z), levels, ''ShowText'',''on'')');
    colorbar; % This displays the color scale
    contour_plot.png

    Solution

    Resulting plot:

    Figure \(\PageIndex{3}\): contour() plot

    .

     

     

     

    .

    colormap Options

    jet

    clipboard_e526502597894336979733c3332b58214.png

    parula

    clipboard_ea2fb962df02488a334e7596498651f32.png

    hsv

    clipboard_e83c92b0220eabff8534dbf4ecd4123f8.png

    hot

    clipboard_e31bcd2ebf6346ebe9413e4f3644f50aa.png

    cool

    clipboard_e17ed93ff8ae70eb715de1a066e1e18af.png

    spring

    clipboard_ecd960a487ea8c2731e19dd2f14099e94.png

    summer

    clipboard_eac44aae693b5fc515f8e3863f2abcb00.png

    autumn

    clipboard_e9844faa3b629d93f685640376ae80956.png

    winter

    clipboard_ee24934e6544dbea2b158cec8b8659180.png

    gray

    clipboard_e2fd0794499b34dc8adf5e2c9e5a70617.png

    bone

    clipboard_e9ca05e472ccb98ef5661bff82e97a1ce.png

    copper

    clipboard_eb31d71a9973063ea4ac30ac02c01f58e.png

    prism

    clipboard_e4bcde22b75fb32be2f4ca0e41a8925be.png

    flag

    clipboard_e9e25c5bb9f0caf707182100d5a99c3c7.png

    colorcube

    clipboard_e6dd532213aa3e507cf1fbaa5faa92df1.png

    lines

    clipboard_e78c482d3bfc843b7e695a31c7cfd599a.png

     


    This page titled 15.2: Plot 3D Surface and Contour Plots is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.

    • Was this article helpful?