2d Plot
2d Plot
Two-Dimensional Plots
2
5.0
3
5.1 THE
plot
COMM
AND
4
5.1 THE plot COMMAND
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
7
5.1 THE plot COMMAND
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
5.1 THE plot COMMAND
10
5.1 THE plot COMMAND
Line Colors
11
5.1 THE plot COMMAND
Marker Types
12
5.1 THE plot COMMAND
13
5.1 THE plot COMMAND
14
5.1 THE plot COMMAND
15
5.1.1
Plot of
Given
Data
16
5.1.2
Plot of a
Functio
n
17
5.1.2
Plot of a
Functio
n
18
5.1.2
Plot of a
Functio
n
19
5.1.2
Plot of a
Functio
n
20
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
22
5.2 THE fplot COMMAND
Example
>> fplot(@(x) x.^2+4*sin(2*x)-1,[-3,3])
23
5.3 PLOTTING MULTIPLE GRAPHS IN THE SAME PLOT
24
5.3.1
Using
the
plot
25
5.3.1
Using
the
plot
Comma
nd
26
5.3.2
Using
the
hold
28
5.3.3
Using
the
line
Comma
nd
29
5.3.3
Using
the
line
Comma
nd
30
5.4 FORMATTING A PLOT
31
5.4.1
Formatt
ing a
Plot
Using
Comma
nds 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)')
32
5.4.1
Formatt
ing a
Plot
Using
• title('Some text') writes title above plot
Comma
nds
33
5.4.1
Formatt
ing a
Plot
Using
Comma
nds
legend('text1','text2',...,pos)
writes legend
• For each graph (data set) displays short line in
same style as graph line and adds specified text
First string goes with first graph plotted, second
string goes with second graph plotted, etc.
• Most useful for plots having at least two graphs
• pos values in book are obsolete as of MATLAB
7.0 (R14). Type help legend or see
documentation for new values
34
5.4.1
Formatt
ing a
Plot
Using
Comma
Formatting the text within the xlabel, ylabel,
nds 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
35
5.4.1
Formatt
ing a
Plot
Using
Comma
nds
36
5.4.1
Formatt
ing a
Plot
Example titles
Using
Comma
nds
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
37
5.4.1
Formatt
ing a
Plot
Example titles
Using
Comma
nds
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
38
5.4.1
Formatt
ing a
Plot
Using
Comma
nds
Some common modifiers
• \bf – bold face
• \it – italic
• \rm – normal font
• \fontname{fontname} – font name
• \fontsize{fontsize} – font size
39
5.4.1
Formatt
ing a
Plot
Using
Comma
nds 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') makes H2O
• ylabel('e^{-k*sin(x)}') makes e-k*sin(x)
40
5.4.1
Formatt
ing a
Plot
Using
Comma
Greek characters:
nds
41
5.4.1
Formatt
ing a
Plot
Using
Comma
ylabel('Standard deviation (\sigma) of resistance in M\Omega')
nds
makes
Standard deviation (σ) of resistance in MΩ
Some Greek characters
42
5.4.1
Formatt
ing a
Plot
Using
Comma
For xlabel, ylabel, title, text,
nds can also change display of entire text string by
using property name – property value pairs,
e.g.,
text(x,y,'Some text',PropertyName,PropertyValue)
• PropertyName is text string
• PropertyValue is number if value is
number or text string if value is letter or
word
Example
th
text(x,y,'Depth','Rotation',45)
ep
makes D 43
5.4.1
Formatt
ing a
Plot
44
5.4.1
Formatt
ing a
Plot
Using
Comma
nds
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
45
5.4.1
Formatt
ing a
Plot
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!)
46
5.4.1
Formatt
ing a
Plot
Using
Comma
nds
The grid command:
grid on
Adds grid lines to plot
grid off
Removes grid lines from plot
47
5.4.1
Formatt
ing a
Plot
This script
Using
Comma
nds
48
5.4.1
Formatt
ing a
Plot
49
5.4.2
Formatt
ing a
Plot
50
5.5 PLOTS WITH LOGARITHMIC AXES
51
5.5
PLOTS
WITH
LOGAR
ITHMI
C AXES
52
5.5 PLOTS WITH LOGARITHMIC AXES
Examples
53
5.6 PLOTS WITH ERROR BARS
errorbar(x,y,e)
• All vectors in the command must be same size
• x and y are horizontal- and vertical-axis data
• e is error measurement at each point
At each y(i), MATLAB draws vertical error bar
from y(i)-e(i) to y(i)+e(i)
55
5.6 PLOTS WITH ERROR BARS
errorbar(x,y,d,u)
• All vectors in the command must be same size
• x and y are horizontal- and vertical-axis data
• u,d are error measurements at each point
At each y(i), MATLAB draws vertical error bar from
y(i)-d(i) to y(i)+u(i)
NOTE: - descriptions for second and third
arrows at bottom of page should be switched
56
5.7 PLOTS WITH SPECIAL GRAPHICS
57
5.7 PLOTS WITH SPECIAL GRAPHICS
58
5.7 PLOTS WITH SPECIAL GRAPHICS
59
5.8 HISTOGRAMS
60
5.8 HISTOGRAMS
61
5.8 HISTOGRAMS
62
5.8 HISTOGRAMS
63
5.8 HISTOGRAMS
64
5.9 POLAR PLOTS
65
5.9 POLAR PLOTS
66
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 upper
Subplot numbers for
left being subplot 1 and lower right 3x2 set of subplots
subplot m*n. p in subplot
command refers to this numbering
67
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 68
5.11 MULTIPLE FIGURE WINDOWS
69
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
70
5.11 MULTIPLE FIGURE WINDOWS
Example
71
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.
72
5.11 MULTIPLE FIGURE WINDOWS
73
5.12 PLOTTING USING THE PLOTS TOOLSTRIP
74
5.12 PLOTTING USING THE PLOTS TOOLSTRIP
75
5.12 PLOTTING USING THE PLOTS TOOLSTRIP
77