3.5: Plotting Points
- Page ID
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.
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
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')
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.
>> clf >> hold on >> plot(1, 1, 'ro') >> plot(2, 2, 'go') >> plot(3, 3, 'bo') >> hold off
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.
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.