Skip to main content

Registration is now open for this year's LibreFest! Join us virtually the week of July 13.

Register here
Engineering LibreTexts

10.3: Creating Simple Plots

  • Page ID
    136712
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

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

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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{\longvect}{\overrightarrow}\)

    \( \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}\)

    The most common plotting function in MATLAB is plot. We can use it to plot a single point, a vector of values, or one vector against another vector.

    Plotting a Single Point

    To plot a single point, we can pass the x-coordinate, the y-coordinate, and an optional style specification. For example, the following command plots the point (2, 3) using a red star:

    Example \(\PageIndex{1}\)

    plot(2, 3, 'r*');

    Solution

    The first value is the x-coordinate, the second value is the y-coordinate, and the third input tells MATLAB how to draw the point. In this case, r means red and * means star marker.

    clipboard_efc95ed751c28e4d740be99180aaf6423.png

     

     

     

    Plotting One Vector

    If you pass one vector to plot, MATLAB uses the vector values as the y-values. The x-values are automatically set to 1, 2, 3, and so on.

    Example \(\PageIndex{2}\)

    y = [2 4 9 10 12];

    plot(y);

    Solution

    This creates a plot with five points. The y-values are 2, 4, 9, 10, and 12. The x-values are 1 through 5 because there are five elements in the vector.clipboard_e69feae9e9d6251e14faf55aff911488b.png

     

    Plotting y Versus x

    Most of the time, we want to control both the x-values and the y-values. In that case, we pass two vectors to plot:

    Example \(\PageIndex{3}\)

    x = [2 4 6 8 10];

    y = [2 4 9 10 12];

    plot(x, y);

    Solution

    Here MATLAB plots each pair of values: (2, 2), (4, 4), (6, 9), (8, 10), and (10, 12).

    By default, MATLAB connects the points with a thin solid blue line.

    clipboard_e419fcf9d484f6e18aa296d07a97871b1.png

     

    Caution

    The x vector and y vector must have the same number of elements. If x has 5 values and y has 6 values, MATLAB will not know how to pair the data points correctly.

     

    Customizing Plot Appearance

    A default MATLAB plot is useful, but it is often not enough for a report or presentation. We usually want to adjust the color, marker, and line style so the plot is easier to read.

    Category

    Examples

    Meaning

    Colors

    'b', 'r', 'g', 'k', 'm', 'c', 'y'

    blue, red, green, black, magenta, cyan, yellow

    Markers

    'o', '*', '+', 'x', 's', 'd', '^'

    circle, star, plus, x-mark, square, diamond, triangle

    Line styles

    '-', '--', ':', '-.'

    solid, dashed, dotted, dash-dot

     

    These symbols can be combined in one style string. For example:

    plot(x, y, 'ro')     % red circle markers
    plot(x, y, 'k--')    % black dashed line
    plot(x, y, 'm*')     % magenta star markers
    plot(x, y, 'gd-')    % green diamonds connected with a solid line

     

    Example \(\PageIndex{4}\)

    Projectile Motion

    Let us use plotting to visualize a basic engineering problem: the trajectory of a projectile. Suppose a ball is launched at an angle with an initial velocity. If we ignore air resistance, the horizontal and vertical positions can be modeled as functions of time.

     

    % Projectile trajectory example
    
    clear; clc; clf;
    
    angle = 45;          % launch angle in degrees
    velocity = 35.0;     % initial velocity in m/s
    g = 9.8;             % gravitational acceleration in m/s^2
    
    
    timeSteps = 0:0.1:5;
    
    
    xPos = velocity * cosd(angle) * timeSteps;
    yPos = velocity * sind(angle) * timeSteps - 0.5 * g * timeSteps.^2;
    
    
    plot(xPos, yPos, 'm*')
    xlabel('Horizontal Position (m)')
    ylabel('Vertical Position (m)')
    title('Projectile Motion')
    grid on
    

    In the above example, notice the use of cosd and sind. These functions use angles measured in degrees. If the angle were in radians, we would use cos and sin instead. Also notice the dot in timeSteps.^2. This tells MATLAB to square each element of the time vector individually.

    Solution

    clipboard_edc3f277732d701843af8a5338ad7c1d1.png

     

     

    Helpful Plot Functions

    MATLAB provides several functions that help us manage figure windows and make plots more descriptive.

    Function

    Purpose

    clf

    Clears the current figure window.

    figure

    Creates a new figure window or makes a specific figure active.

    hold on

    Keeps the current plot so that new plots are added to the same axes.

    hold off

    Turns off hold so new plots replace the old plot.

    legend

    Adds a legend that identifies plotted curves.

    grid on

    Adds grid lines to the plot.

    xlabel

    Adds a label below the x-axis.

    ylabel

    Adds a label next to the y-axis.

    title

    Adds a title above the plot.

     

    Good habit

    Every plot that you turn in for a lab report, homework assignment, or project should usually have a title, x-axis label, y-axis label, and, when needed, a legend.

     

    Plotting Multiple Curves on One Figure

    To plot more than one curve on the same axes, use hold on after the first plot. Then create the additional plots. When you are finished, use hold off.

     

    Example \(\PageIndex{5}\)

    Plotting four curves on the same figure.

    clear; clc; clf;
    
    x = 1:5;
    y1 = [2 5 6 8 11];
    y2 = [4 3 5 7 8];
    y3 = [1 6 9 12 9];
    y4 = [4 4 4 4 4];
    
    
    figure(1)
    plot(x, y1, 'k')
    hold on
    plot(x, y2, 'ko')
    plot(x, y3, 'gd')
    plot(x, y4, 'r--')
    hold off
    
    
    grid on
    legend('y1', 'y2', 'y3', 'y4')
    xlabel('x values')
    ylabel('y values')
    title('Four Curves on One Plot')
    
    Solution

    clipboard_e5f6470b72837b2219d7bc12ed8080872.png

    The order of labels in the legend matters. MATLAB matches the first legend label to the first plot, the second label to the second plot, and so on.

    Exercise \(\PageIndex{1}\)

    In MATLAB plot sin(x) and cos(x) on the same figure. Use x values ranging between 0 and 2\(\pi\). Add a title, legend, x-axis label, and y-axis label to the figure.


    10.3: Creating Simple Plots is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?