Skip to main content
Engineering LibreTexts

3.5: Plotting Points

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

    If the output of your program is a long stream of numbers, it can be hard to see what is happening. Plotting the results can make things clearer.

    The plot function is a versatile tool for plotting two-dimensional graphs. Unfortunately, it’s so versatile that it can be hard to use (and hard to read the documentation). We’ll start simple and work our way up.

    Graphics Toolkit for Octave

    If you are using Octave, you need to activate a graphics toolkit before creating a plot. Usually one of these 2 options will work, depending on your computer's graphics and on your version of Octave:


    Use only "qt" or "fltk", not both. More information can be found at:

    The rest of this section applies to both MATLAB and Octave.

    Plot a single point with figure() and plot()

    Use the figure function to create a figure window.

    To plot a single point, type

    >> plot(1, 2, 'o')

    A Figure Window should appear with a graph and a single blue circle at \(x\) position 1 and \(y\) position 2.

    The letter in single quotes is a style string that specifies how the point should be plotted; o indicates a circle. Other shapes include +, *, x, s (for a square), d (for a diamond), and ^ (for a triangle).

    You can also specify the color by starting the style string with a color code:

    >> plot(1, 2, 'ro')

    Here, r stands for red; the other colors include g for green, b for blue, c for cyan, m for magenta, y for yellow, and k for black.

    When you use plot this way, it can only plot one point at a time. If you run plot again, it clears the figure before making the new plot. The hold command lets you override that behavior: hold on tells MATLAB not to clear the figure when it makes a new plot; hold off returns to the default behavior.

    Try this:

    >> clf
    >> hold on
    >> plot(1, 1, 'ro')
    >> plot(2, 2, 'go')
    >> plot(3, 3, 'bo')
    >> hold off

    The clf command clears the figure before we start plotting.

    If you run the code above, you should see a figure with three circles. MATLAB scales the plot automatically so that the axes run from the lowest values in the plot to the highest.

    Exercise \(3.2\)

    Modify bikeloop.m so that it clears the figure before running the loop. Then, each time through the loop, it should plot the value of b versus the value of i with a red circle.

    Once you get that working, modify it so it plots the values of c with blue diamonds.

    This page titled 3.5: Plotting Points is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.