Lecture 3 - Plotting
Lecture 3 - Plotting
PLOTTING
2-D Plotting
Time Distance
x-y plot: (sec) (Ft)
Title
To add an apostrophe to a title (or other annotation) you must enter the single
quote twice – otherwise MATLAB interprets the single apostrophe as the end of
the string.
X axis label, Y axis label
Often it is useful to add a grid
MATLAB overwrites the figure window every time you request a new plot
To open a new figure window, use the figure function – for example figure(2)
Plots with multiple lines (hold on)
hold on
Freezes the current plot, so that an
additional plot can be overlaid
When you use this approach, the
additional line is drawn in red – the
default drawing color is blue
To unfreeze the plot, use the hold off
command
Plots with multiple lines using one
command
You can also create multiple lines on a single graph with one command.
Each set of ordered pairs will produce a new line.
Plots with multiple lines with the same x
value
If you use the plot command with a single matrix, MATLAB plots the values
versus the index number
Usually, this type of data is plotted on a bar graph
When plotted on an x-y grid, it is often called a line graph
Try plot(peaks(100))
If you try to use two arrays of complex numbers in the plot function, the
imaginary components are ignored
Line, Color and Mark Style
point .
You can change the appearance of
your plots by selecting: circle o
x-mark x
line styles, color, mark styles
Color Indicator plus +
If you don’t specify style, the default is
star *
used which is: line style – none, mark blue b
style – none, and color – blue green g
square s
triangle down v
For example Line Style Indicator cyan c
triangle up ^
plot(x,y,':ok’) solid - magent m
triangle left <
a
dotted line, circles, black dotted :
yellow y triangle right >
dash-dot -.
pentagram p
dashed -- black k
hexagram h
Axis scaling
Use Greek letters in your labels by putting a backslash (\) before the name
of the letter.
title(‘\alpha \beta \gamma’) creates the plot title α β γ
The subplot command allows you to subdivide the graphing window into a
grid of m rows and n columns.
subplot(m,n,p)
m = rows, n = columns, p = location.
Example: subplot(2,2,1)
Peaks
-5 2
2
0 2
0
-2 -2
y x
3 4
Other Types of 2-D Plots
Defaults to 10 bins
X-Y Graphs with Two Y Axes
Sometimes it is useful to overlay two x-y plots onto the same figure.
However, if the order of magnitude of the y values are quite different, it
may be difficult to see how the data behave.
Scaling Depends on the largest value plotted.
It is difficult to see how the blue line behaves, because the scale isn’t
appropriate.
X-Y Graphs with Two Y Axes - plotyy
The plotyy function allows you to use two scales on a single graph
X-Y Graphs with Two Y Axes – Labels
Both mesh and surf can be used to good effect with a single two-dimensional matrix
Surface Plots – mesh
If x and y are unknown, the x and y coordinates are the matrix index numbers
If we know the values of x and y that correspond to our z values, we can plot against
those values instead of the index numbers
Surface Plots – surf