14.2: Plot 3D Surface and Contour Plots (2024)

  1. Last updated
  2. Save as PDF
  • 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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

    \( \newcommand{\vectorC}[1]{\textbf{#1}}\)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}}\)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}\)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}}}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{#1}}} \)

    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')

    Solution14.2: Plot 3D Surface and Contour Plots (2)

    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')
    14.2: Plot 3D Surface and Contour Plots (3)

    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
    14.2: Plot 3D Surface and Contour Plots (4)

    Solution

    Resulting plot:

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

    .

    .

    colormap Options

    jet

    14.2: Plot 3D Surface and Contour Plots (5)

    parula

    14.2: Plot 3D Surface and Contour Plots (6)

    hsv

    14.2: Plot 3D Surface and Contour Plots (7)

    hot

    14.2: Plot 3D Surface and Contour Plots (8)

    cool

    14.2: Plot 3D Surface and Contour Plots (9)

    spring

    14.2: Plot 3D Surface and Contour Plots (10)

    summer

    14.2: Plot 3D Surface and Contour Plots (11)

    autumn

    14.2: Plot 3D Surface and Contour Plots (12)

    winter

    14.2: Plot 3D Surface and Contour Plots (13)

    gray

    14.2: Plot 3D Surface and Contour Plots (14)

    bone

    14.2: Plot 3D Surface and Contour Plots (15)

    copper

    14.2: Plot 3D Surface and Contour Plots (16)

    prism

    14.2: Plot 3D Surface and Contour Plots (17)

    flag

    14.2: Plot 3D Surface and Contour Plots (18)

    colorcube

    14.2: Plot 3D Surface and Contour Plots (19)

    lines

    14.2: Plot 3D Surface and Contour Plots (20)
    14.2: Plot 3D Surface and Contour Plots (2024)

    References

    Top Articles
    Latest Posts
    Article information

    Author: Greg O'Connell

    Last Updated:

    Views: 5622

    Rating: 4.1 / 5 (62 voted)

    Reviews: 93% of readers found this page helpful

    Author information

    Name: Greg O'Connell

    Birthday: 1992-01-10

    Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

    Phone: +2614651609714

    Job: Education Developer

    Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

    Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.