Skip to main content
Engineering LibreTexts

1.3: Plotting in MATLAB

  • Page ID
    9461
  • \( \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}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    A picture is worth a thousand words, particularly visual representation of data in engineering is very useful. MATLAB has powerful graphics tools and there is a very helpful section devoted to graphics in MATLAB Help: Graphics. Students are encouraged to study that section; what follows is a brief summary of the main plotting features.

    Two-Dimensional Plots

    The

    plot
    

    Statement

    Probably the most common method for creating a plot is by issuing plot(x, y) statement where function y is plotted against x.

    Type in the following statement at the MATLAB prompt:

    x=[-pi:.1:pi]; y=sin(x); plot(x,y);
    

    After we executed the statement above, a plot named Figure1 is generated:

    屏幕快照 2019-05-25 19.59.41.png

    \(\PageIndex{1}\) Graph of sin(x)

    Having variables assigned in the Workspace, x and y=sin(x) in our case, we can also select x and y, and right click on the selected variables. This opens a menu from which we choose plot(x,y). See the figure below.

    屏幕快照 2019-05-25 20.00.49.png

    \(\PageIndex{2}\) Creating a plot from Workspace.

    Annotating Plots

    Graphs without labels are incomplete and labeling elements such as plot title, labels for x and y axes, and legend should be included. Using up arrow, recall the statement above and add the annotation commands as shown below.

    ParseError: invalid XmlNode (click for details)
    Callstack:
        at (Bookshelves/Computer_Science/Applied_Programming/A_Brief_Introduction_to_Engineering_Computation_with_MATLAB_(Beyenir)/01:_Chapters/1.03:_Plotting_in_MATLAB), /content/body/div[1]/div[3]/div[1]/pre, line 2, column 41
    

    Run the file and compare your result with the first one.

    屏幕快照 2019-05-25 21.02.36.png

    \(\PageIndex{3}\) Graph of sin(x) with Labels.

    aside:

    Type in the following at the MATLAB prompt and learn additional commands to annotate plots:

    help gtext
    help legend
    help zlabel
    

    Superimposed Plots

    If you want to merge data from two graphs, rather than create a new graph from scratch, you can superimpose the two using a simple trick:

    ParseError: invalid XmlNode (click for details)
    Callstack:
        at (Bookshelves/Computer_Science/Applied_Programming/A_Brief_Introduction_to_Engineering_Computation_with_MATLAB_(Beyenir)/01:_Chapters/1.03:_Plotting_in_MATLAB), /content/body/div[1]/div[4]/div/pre, line 9, column 8
    

    屏幕快照 2019-05-25 20.07.15.png

    \(\PageIndex{4}\) Graph of sin(x) and cos(x) in the same plot with labels and legend.

    Multiple Plots in a Figure

    Multiple plots in a single figure can be generated with subplot in the Command Window. However, this time we will use the built-in Plot Tools. Before we initialize that tool set, let us create the necessary variables using the following script:

    % This script generates sin(x) and cos(x) variables
    clc                 %Clears command window
    clear all           %Clears the variable space
    close all           %Closes all figures
    X1=[-2*pi:.1:2*pi];  %Creates a row vector from -2*pi to 2*pi with .1 increments
    Y1=sin(X1);          %Calculates sine value for each x
    Y2=cos(X1);          %Calculates cosine value for each x
    Y3=Y1+Y2;            %Calculates sin(x)+cos(x)
    Y4=Y1-Y2;            %Calculates sin(x)-cos(x)
    

    Note that the above script clears the command window and variable workspace. It also closes any open Figures. After running the script, we will have X1, Y1, Y2, Y3 and Y4 loaded in the workspace. Next, select File > New > Figure, a new Figure window will open. Click "Show Plot Tools and Dock Figure" on the tool bar.

    屏幕快照 2019-05-25 20.13.49.png

    \(\PageIndex{5}\) Plot Tools

    Under New Subplots > 2D Axes, select four vertical boxes that will create four subplots in one figure. Also notice, the five variables we created earlier are listed under Variables.

    屏幕快照 2019-05-25 20.16.00.png

    \(\PageIndex{6}\) Creating four sub plots.

    After the subplots have been created, select the first supblot and click on "Add Data". In the dialog box, set X Data Source to X1 and Y Data Source to Y1. Repeat this step for the remaining subplots paying attention to Y Data Source (Y2, Y3 and Y4 need to be selected in the subsequent steps while X1 is always the X Data Source).

    屏幕快照 2019-05-25 20.17.03.png

    \(\PageIndex{7}\) Adding data to axes.

    Next, select the first item in "Plot Browser" and activate the "Property Editor". Fill out the fields as shown in the figure below. Repeat this step for all subplots.

    屏幕快照 2019-05-25 20.18.15.png

    \(\PageIndex{8}\) Using "Property Editor".

    Save the figure as sinxcosx.fig in the current directory.

    屏幕快照 2019-05-25 20.19.33.png

    \(\PageIndex{9}\) The four subplots generated with "Plot Tools".

    屏幕快照 2019-05-25 20.20.32.png

    \(\PageIndex{10}\) The four subplots in a single figure.

    Three-Dimensional Plots

    3D plots can be generated from the Command Window as well as by GUI alternatives. This time, we will go back to the Command Window.

    The

    plot3
    

    Statement

    With the X1,Y1,Y2 and Y2 variables still in the workspace, type in plot3(X1,Y1,Y2) at the MATLAB prompt. A figure will be generated, click "Show Plot Tools and Dock Figure".

    屏幕快照 2019-05-25 20.22.44.png

    \(\PageIndex{11}\) A raw 3D figure is generated with plot3.

    Use the property editor to make the following changes.

    屏幕快照 2019-05-25 20.23.57.png

    \(\PageIndex{12}\) 3D Property Editor.

    The final result should look like this:
    屏幕快照 2019-05-25 20.25.22.png

    \(\PageIndex{13}\) 3D graph of x, sin(x), cos(x)

    Use help or doc commands to learn more about 3D plots, for example, image(x), surf(x) and mesh(x).

    Quiver or Velocity Plots

    To plot vectors, it is useful to draw arrows so that the direction of the arrow points the direction of the vector and the length of the arrow is vector’s magnitude. However the standard plot function is not suitable for this purpose. Fortunately, MATLAB has quiverfunction appropriately named to plot arrows. quiver(x,y,u,v) plots vectors as arrows at the coordinates (x,y) with components (u,v). The matrices x, y, u, and v must all be the same size and contain corresponding position and velocity components.

    Calculate the magnitude of forces OA, OB and the resultant R of OA and OB shown below. Plot all three forces on x-y Cartesian coordinate system1.

    屏幕快照 2019-05-25 20.36.03.png

    \(\PageIndex{14}\) Quiver Plot.

    ParseError: invalid XmlNode (click for details)
    Callstack:
        at (Bookshelves/Computer_Science/Applied_Programming/A_Brief_Introduction_to_Engineering_Computation_with_MATLAB_(Beyenir)/01:_Chapters/1.03:_Plotting_in_MATLAB), /content/body/div[2]/div/div[2]/div[2]/div[1]/div[1]/pre, line 16, column 8
    
    屏幕快照 2019-05-25 20.42.19.png
    \(\PageIndex{15}\) Output of quiver function.

    Write an interactive script to calculate the resultant R of forces F1, F2 and F3 shown below and plot all four forces on x-y Cartesian coordinate system2.

    屏幕快照 2019-05-25 20.44.30.png

    \(\PageIndex{16}\) An example for quiver3 plot.

    ParseError: invalid XmlNode (click for details)
    Callstack:
        at (Bookshelves/Computer_Science/Applied_Programming/A_Brief_Introduction_to_Engineering_Computation_with_MATLAB_(Beyenir)/01:_Chapters/1.03:_Plotting_in_MATLAB), /content/body/div[2]/div/div[2]/div[2]/div[2]/div/pre, line 4, column 7
    

    屏幕快照 2019-05-25 20.45.52.png

    \(\PageIndex{17}\) Output of quiver3 function.

    Summary of Key Points

    1. plot(x, y) and plot3(X1,Y1,Y2) statements create 2- and 3-D graphs respectively,
    2. Plots at minimum should contain the following elements: title, xlabel, ylabel and legend,
    3. Annotated plots can be easily generated with GUI Plot Tools,
    4. quiver and quiver3 plots are useful for making vector diagrams.

    Footnotes

    • 1 Applied Engineering Mechanics by A. Jensen, H. Chenoweth McGraw-Hill Ryerson Limited © 1972, (p. 15)
    • 2 Applied Engineering Mechanics by A. Jensen, H. Chenoweth McGraw-Hill Ryerson Limited © 1972, (p. 15)

    This page titled 1.3: Plotting in MATLAB is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Serhat Beyenir via source content that was edited to the style and standards of the LibreTexts platform.

    • Was this article helpful?