Chapter 5
Two-Dimensional Plots
MATLAB An Introduction With Applications, 5th Edition Slide deck by
Dr. Amos Gilat Dr. Greg Reese
The Ohio State University Miami University
5.0
This chapter will cover 2D (two-
dimensional) plots. Many options:
▪Linear, semi-logarithmic, logarithmic axes
▪Line type, color, thickness
▪Lots of different data-point markers
▪Grid lines, titles, text comments, legends
▪Subplots
▪Bar, stair, polar plots
2
5.0
3
5.1 THE plot COMMAND
plot command used to make basic 2D
plots. Simplest form is
plot(y)
▪Plots vector y on vertical axis, numbers 1
through N on horizontal axis (N = number
of points in y)
▪If there's a Figure Window, draws in it.
Otherwise, creates a new Figure Window
and draws in that
4
5.1 The plot Command
plot(y) default values
▪Both axes linear
▪ MATLAB chooses axis ranges so that
end values are nice
▪Points connected by straight lines
▪No point markers
▪Points and lines in blue
5
5.1 The plot Command
Example
>> y = [ 2.1 2.1 2.1 1 0 -1 -2.1 -2.1 -2.1 ];
>> plot( y )
6
5.1 The plot Command
If after issuing plot command, no
Figure Window appears, window
TIP is buried. Click on Figure Window
icon on task bar to make window
appear
7
5.1 The plot Command
Second simplest form is
plot(x,y)
▪x and y are vectors that have same size
(number of elements) but any
dimension
▪x-values on horizontal axis, y-values on
vertical axis
▪Default values same as for plot(x)
8
5.1 The plot Command
Example
>> x=[1.1 1.8 3.2 5.5 7 7.5 8 10];
>> y=[2 6.5 7 7 5.5 4 6 8];
>> plot(x,y)
9
Example
10
5.1 The plot Command
To use values other than defaults,
11
5.1 The plot Command
Line specifiers define style and color of
lines, and marker types
Line Styles
Line Colors
12
5.1 The plot Command
Marker Types
13
5.1 The plot Command
14
5.1 The plot Command
Property Name and Property Value:
▪ In plot command, type property name in
quote marks, then comma, then value
15
Exampe
16
5.1.2 Plot of a Function
One way to plot a function of an
independent variable:
1. Create a vector of values of
the independent variable
2. Create a vector of value of
function at every element of
above vector
3. Plot using plot(x,y)
17
5.1.2 Plot of a Function
Increment: 0.01 Increment: 0.3
18
5.1.2 Plot of a Function
To copy entire Figure Window into
another program, e.g., Word,
PowerPoint
1. Click on Figure Window to make it
current window
2. Select ALT+PRNTSCRN
3. Paste into other application
19
5.1.2 Plot of a Function
To copy just plot area of Figure
Window into another program,
e.g., Word, PowerPoint
1. In Figure Window, select
Edit, then Copy Figure
2. Paste into other application
20
5.2 The fplot Command
fplot plots a function y = f(x)
between given limits
▪Can specify function in writing,
i.e., as a text string
▪Function can include MATLAB's
functions or ones that you write
21
Example
22
5.2 The fplot Command
fplot('function',limits,'line specifiers')
▪Independent variable in function can be
any letter, e.g.,
▪ 8*x^2+5*cos(x) or 8*t^2+5*cos(t)
▪limits – two- or four-element vector
that specifies plot axes limits
▪ [xmin xmax] – limits on x-axis
▪ [xmin xmax ymin ymax] – limits on both
axes
▪line specifiers – same as in plot
23
5.2 The fplot Command
Example
>> fplot('x^2+4*sin(2*x)-1',[-3,3])
24
Example
25
Example
26
5.3 Plotting Multiple Graphs in the Same Plot
Often want to graph more than one
set of data on the same plot
MATLAB can do this three
different ways
27
5.3.1 Using the plot Command
Plot two more graphs on same plot as
follows (example for three graphs)
plot(x,y,u,v,t,h)
▪Plots y vs. x, v vs. u, h vs. t
▪Vectors of each pair must be same
size
▪ Can be different than sizes in other pairs
▪Can use line specifiers by putting in
triplets (x-data, y-data, specifier), e.g.,
plot(x,y,'-b', u,v,'--r','t,h,'g:')
28
Example
29
5.3.2 Using the hold on and hold off Commands
Normally, each time you execute plot
it erases previous plot and draws new
one. To change this behavior:
▪Draw the first graph with plot
▪Issue the command hold on
▪Call plot for each of the remaining
graphs
▪Issue the command hold off
Graphs drawn after hold on are
added to plot. Graphs drawn after
hold off erase plot
30
5.3.2 Using the hold on and hold off Commands
31
5.3.3 Using the line Command
line command adds additional
graphs to an existing plot
line(x,y,'PropertyName','PropertyValue')
Example
line(x,y,'linestyle','--','color','r','marker','o')
adds graph drawn with dashed red
line and circular markers to current
plot
32
5.3.3 Using the line Command
33
5.4 Formatting a Plot
Will learn how to spruce up a plot
by adding
▪Axis labels
▪Title
▪Legend
▪Text
▪Grid
▪Custom axis ranges
34
5.4.1 Formatting a Plot Using Commands
plot or fplot make basic plot. After
issuing that command, can use
▪xlabel('some text') writes
label below horizontal axis
▪Example: xlabel('Time (sec)')
▪ylabel('some text') writes
label to left of vertical axis
▪Example: ylabel('Current (mA)')
35
5.4.1 Formatting a Plot Using Commands
▪title('Some text') writes title above
plot
▪ Example: title('Diode Current')
▪text(x,y,'Some text') places text in
figure with first character at (x,y)
▪ Example:
text(x,y,'Peak 3.5 sec after first')
▪gtext('Some text') – figure window
opens, user clicks on graph where she wants
text to appear
36
5.4.1 Formatting a Plot Using Commands
Formatting the text within the xlabel, ylabel, title,
text and legend commands:
Can format text displayed by above
commands
▪Can set font, size, character color,
background color, sub/superscript, style
(bold, italic, etc.)
▪Can display Greek letters
▪Can format using modifiers within text string
or by adding property names and values to
command
37
5.4.1 Formatting a Plot Using Commands
Example titles
title('\it What You Should Never See')
makes
What You Should Never See
title('What You Should{\it Never} See')
makes
What You Should Never See
38
5.4.1 Formatting a Plot Using Commands
Example titles
title('\fontname{Old English Text MT}...
My Name is Robin Hood')
makes
My Name is Robin Hood
title(...
'{\fontname{Old English Text MT}Robin Hood}...
was here')
makes
Robin Hood was here
39
5.4.1 Formatting a Plot Using Commands
Some common modifiers
▪\bf – bold face
▪\it – italic
▪\rm – normal font
▪\fontname{fontname} – font name
▪\fontsize{fontsize} – font size
40
5.4.1 Formatting a Plot Using Commands
Subscript and superscript:
To make single character
▪Subscript – precede it by underscore (_)
▪Superscript – precede it by caret(^)
For multiple characters, same as above
but enclose characters in (curly) braces
▪ xlabel('H_2O (l)') makes H2O
▪ ylabel('e^{-k*sin(x)}') makes e-k*sin(x)
41
5.4.1 Formatting a Plot Using Commands
ylabel('Standard deviation (\sigma) of resistance in M\Omega')
makes
Standard deviation (σ) of resistance in MΩ
Some Greek characters
42
5.4.1 Formatting a Plot Using Commands
Some property-name property-value pairs
43
Example (plot is in the 41th slide)
This script
44
Example
45
5.4.1 Formatting a Plot Using Commands
The axis command:
MATLAB makes axes limits in
plot command so that all data
appears and limits are nice
numbers. Can change that with
axis command
46
5.4.1 Formatting a Plot Using Commands
Common axis variations are:
axis([xmin xmax ymin ymax])
▪ Sets limits of both axes
axis equal
▪ Sets same scale for both axes
axis square
▪ Sets axis region to be square
axis tight
▪ Sets axes limits to range of data
(not usually nice numbers!)
47
5.4.1 Formatting a Plot Using Commands
The grid command:
grid on
▪ Adds grid lines to plot
grid off
▪ Removes grid lines from plot
48
Example
49
5.5 Plots With Logarithmic Axes
Often use plot with one or both axes
being logarithmic (log)
▪Used to display data with large
range of values
▪Used to make some functional
relationships more apparent
▪ For example, y = 10(2x+3) is a straight line on
a semilog plot
50
5.5 PLOTS WITH LOGARITHMIC AXES
MATLAB commands for log plots
▪Can use line specifiers and property-
name property-value pairs as in plot
▪On logarithmic axis, make sure all data
is > 0 because otherwise log is
undefined
51
5.5 Plots With Logarithmic Axes
Example
52
5.7 Plots With Special Graphics
Examples of specialized plots
53
5.7 Plots With Special Graphics
54
5.10 Putting Multiple Plots on the Same Page
subplot(m,n,p)
divides Figure Window into m
rows and n columns of
subplots
▪Subplots numbered from left to
right and top to bottom, with Subplot numbers for
upper left being subplot 1 and 3x2 set of subplots
lower right subplot m*n. p in
subplot command refers to this
numbering
55
5.10 Putting Multiple Plots on the Same Page
subplot(m,n,p)
▪If subplots don't exist, subplot creates
them and makes subplot p the current
subplot
▪If subplots exist, subplot makes subplot p
the current one
▪When subplot defines current subplot,
next plot and formatting commands draw
in current subplot
See Sample Problem 5-2 for example of
subplot use
56
Example
57
5.11 Multiple Figure Windows
On execution, any plotting command
1. Creates a Figure Window (if none exists)
2. Erases any plot in that window
3. Draws new plot
Can be useful though to have plots in
multiple windows. figure command
lets you do this
58
5.11 Multiple Figure Windows
figure
1. Creates a new Figure Window
2. Labels the window "Figure n"
▪ n such that first window is Figure 1,
second is Figure 2, etc.
3. Makes new window the active
Figure Window
4. Brings window to front of the screen
Subsequent plotting commands
draw in the active Figure Window
59
5.11 Multiple Figure Windows
Example
60
5.11 Multiple Figure Windows
figure(n)
▪If Figure Window n exists, makes it the
active window
▪If Figure Window n doesn't exist, creates
it and makes it the active window
▪figure(n) useful in scripts, e.g., scripts
in which data set 1 is displayed in Figure
1, data set 2 is displayed in Figure 2, etc.
61
5.11 Multiple Figure Windows
Use close command to close
figure windows
▪close closes active Figure Window
▪close(n) closes Figure Window n
▪close all closes all open Figure
Windows
62