Skip to main content
Library homepage
 
Engineering LibreTexts

3.5: Plotting Points

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

    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.

    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 Carey Smith via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.