The versatile plot
command is used to create 2D line and scatter plots.
Refer to the on-line reference,
e.g. "doc plot
" or "help plot
", for more details on any of the commands
listed below.
A Note on Graphics
MATLAB makes it easy to create plots from data sets. Although the default for each plot type is a good start, you should also consider whether and how adjustments to the defaults may alter the way that the graphical information is received by the viewer.
One of the most basic factors to consider is the scaling of the axes and whether zero should be included on the axis limits. We won't delve into these issues on this web page. The reader is urged to consider the arguments presented on these sites:
- Visualization: Misleading axes on graphs
- Wikipedia page on Misleading graphs
- Real life examples of misleading graphs from Statitisticshowto.com
MATLAB Commands
The basic commands for creating 2D plots in MATLAB are
plot
for creating 2D plots on linear axessemilogx
,semilogy
,loglog
for creating 2D plots with logarithmic axesaxis
,xlim
,ylim
for changing the scale on the x and y axesxlabel
,ylabel
for adding axis labelslegend
for adding a legend when a 2D plot has more than one data setsubplot
for creating more than one plot in a figure windowfigure
for opening more than one figure window at a time
plot
: Create a two-dimensional plot
In each of the following statements, x
and y
are vectors with the same
number of elements. In other words, length(x)
and length(y)
are equal.
>> plot(x,y) % Plot (x,y) pairs, connect points with lines
>> plot(x,y,'.') % Plot (x,y) pairs with dots at each point
If only two arguments are provided, the data pairs are connected with a solid black line.
The third argument is a string that specifies whether (or not) each data pair is identified with a symbol, whether (or not) the data pairs are connected by lines, and the line type if lines are used.
Here is an excerpt from the output returned by "help plot
"
Various line types, plot symbols and colors may be obtained with
plot(X,Y,S) where S is a character string made from one element
from any or all the following 3 columns:
b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
Multiple sets of data pairs can be plotted with a single plot
command.
Consider the following, where x
and y
are vectors with the same
number of elements, and v
and w
are vectors with the same number of elements.
In other words, length(x)
and length(y)
are equal, length(v)
and length(w)
are equal, but length(x)
and length(v)
are not necessarily equal.
>> plot(x,y,v,w) % Plot (x,y) pairs connected w/ lines
% and (v,w) pairs connected with lines
>> plot(x,y,'k--',v,w,'bo') % plot (x,y) pairs with the points
% connected by dashed black lines;
% and plot (v,w)pairs as points identified with blue circles
When x
and y
have a large number of points, say more than 50, and the
plot(x,y)
command is used, the plot tends to appear as a smooth curve.
However, the plot is just a set of straight line segments connected pairs
of data points. Consider the smooth plot of the sine function
x = linspace(0,2*pi);
y = sin(x);
plot(x,y)
If fewer points are used, a plot of the sine function clearly shows
that plot(x,y)
connects the data pairs with straight line segments
x = linspace(0,2*pi,7);
y = sin(x);
plot(x,y)
If any of the data pairs have NaN
(not-a-number) values, the data pair
is omitted from the plot
x = linspace(0,2*pi,7);
y = sin(x);
y(4) = NaN;
plot(x,y)
xlabel
, ylabel
: Add x and y labels to an existing plot
The xlabel
and ylabel
commands take a single argument, which is a character
string.
>> x = ...; y = ...;
>> plot(x,y)
>> xlabel('X values');
>> ylabel('y = f(x)')
The character string can contain LaTeX codes for Greek letters such as
\alpha
, \beta
, \gamma
, \theta
. For example, the following statements
create a plot of and label the axes with the \theta
symbol.
x = linspace(0,2*pi);
y = sin(x);
plot(x,y);
xlabel('\theta')
ylabel('sin(\theta)')
Note that Greek symbols are only used to label output. All MATLAB variables are plain text.
For more information consult the help page on the Mathworks web site.
axis
: Change the range of the x and y axes
The axis
command changes the limits of both the x and y axes.
>> x = ...; y = ...;
>> plot(x,y)
>> axis([-2 4 0 5]) % Set x-axis limits to [-2, 4] and y-axis limits to [0, 5]
xlim
, ylim
: Change the range of the x or y axes
The xlim
and ylim
commands independently change the limits of the x and y axes.
>> x = ...; y = ...;
>> plot(x,y)
>> xlim([-2 4] ) % Set only the x-axis limits to [-2, 4]
>> ylim([0 5]) % Set only the y-axis limits to [0, 5]
You may ask, "Why use two commands xlim
and ylim
when the axis
command does the same
thing in just one command? Well, if you want to change the scale of both the
x axis and the y axis, it makes sense to use the axis
command. However, there
are situations where you only want to control one axis scale and let MATLAB
automatically decide the other axis scale. In those situations, it makes sense
to use xlim
or ylim
.
subplot
: Put multiple plots in a single figure window
The following example shows how to create two subplots, one above the other.
>> subplot(2,1,1)
>> plot( ... ) % some 2D plot
>> subplot(2,2,1)
>> plot( ... ) % another 2D plot
Note that subplot
has two roles. First, it tells MATLAB to put multiple
plots in an array, and second it tells MATLAB which array location to use
for the next plot.
subplot(m,n,1)
: The next plot is the first plot in the m
by n
array of plots.
subplot(m,n,2)
: The next plot is the second plot in the m
by n
array of plots.
See also the figure
command, below.
close
: Close open plot windows
>> close('all') % close all open plot windows
>> close all
The close('all')
command can be a convenient way to cause MATLAB to open
a new plot window on top of the command window and any open editor windows.
For example, suppose a function called make_my_plot
creates a plot. If
the following composite (two commands on one line) command is entered, any
open plot windows are closed and a new plot window will be drawn on top
of any open MATLAB windows.
>> close all; make_my_plot
figure
: Open a new plot window
>> figure % open a new plot window
>> figure('Name','My data') % open a new plot window with
% 'My data' in the title bar
Using named windows can be helpful when many plot windows are opened by a single m-file.
The figure
command allows multiple plot windows to be open at the same
time. The following example shows how two figure windows could be opened
and each given a descriptive name in the title bar.
>> figure('Name','Plot of approximation to f(x)')
>> plot(x,y)
>> figure('Name','Plot of error in approximation')
>> plot(x,y-yexact)
Using the figure
command to open a new plot window should
not be confused with using the subplot
command to put multiple plots
in the same window.