Professional Documents
Culture Documents
Matlab Graphics
1
Graphics
Function Description
plot
l t Graph
G h 2-D
2 D data
d t with
ith linear
li scales
l for
f both
b th axes
plot3 Graph 3-D data with linear scales for both axes
loglog Graph with logarithmic scales for both axes
Graph with a logarithmic scale for the x-axis and
semilogx
a linear scale for the y-axis
Graph with a logarithmic scale for the y-axis and
semilogy
a linear scale for the x-axis
plotyy Graph with y-tick labels on the left and right side
plot
Linear 2-D plot
plot(Y) plots the columns of Y versus their index if Y is a real number. If
Y is complex, plot(Y) is equivalent to plot(real(Y),imag(Y)). In all
other uses of plot, the imaginary component is ignored.
2
plot
Example
Plot of a sine waves in the range
x = - pi : 0.1 : pi ; 1
0.8
y = sin ( x ) ; 0.6
0.4
plot (x , y) 0.2
-0.2
-0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
5
plot
Adding Titles, Axis Labels, and Line Colors
MATLAB enables you to add axis labels and titles
titles. For
example, using the graph from the previous example,
add an x- and y-axis label:
plot (x , y , ' ro- ')
( p \leq
xlabel('-\pi q \Theta \leq
q \pi')
p )
ylabel('sin(\Theta)')
title('Plot of sin(\Theta)')
3
plot
Plot of sin(Θ)
1
0.8
0.6
0.4
0.2
sin(Θ)
-0.2
-0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
-π ≤ Θ ≤ π
7
4
Improving your labels
y You can use Greek letters in your labels by putting a
backslash ((\)) before the name of the letter
\sigma σ \Sigma Σ \Phi Φ
\delta δ \Delta ∆ \infty ∞
\leq ≤ \geq ≥ \neq ≠
title(‘x^{2}’)
gives x2
plot
Adding Titles, Axis Labels, and Line Colors
The instructions
Th i t ti
title,
0.55
ylabel, 0.5
0.4
text 0.35
0.3
are used to add text 0.25
0.2
to a figure in several 0.15
positions 0.1
0.05
10
5
plot
Adding Titles, Axis Labels, and Line Colors
MATLAB enables you to add axis labels and titles
titles. For
example, using the graph from the previous example,
add an x- and y-axis label: Line Width (default: 0.5)
plot
Plot of sin(Θ)
1
0.8
0.6
0.4
0.2
sin(Θ)
0 ← sin(π)
-0.2
-0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
-π ≤ Θ ≤ π 12
6
plot
Plotting two curves on the same figures.
x=-p
pi : 0.1 : p
pi ;
y = sin ( x ) ;
z = tan(sin(x)) - sin(tan(x));
Plot of two carves
3
y&z
0
-3
-4 -3 -2 -1 0 1 2 3 4
-π ≤ Θ ≤ π 13
plot
x = - pi : 0.1 : pi ;
y = sin ( x ) ;
z = tan(sin(x)) - sin(tan(x));
plot (x , y , ' ro- ', x , z , ' b.: ')
Plot of two carves
3
legend('y-function','z-function')
y-function
-2 z-function
-3
-4 -3 -2 -1 0 1 2 3 4
-π ≤ Θ ≤ π 14
7
x = - pi : 0.1 : pi ; plot 1
Plot of sin(Θ)
y = sin ( x ) ; 0.8
0.6
0.4
sin(Θ)
0
-0.4
-0.8
-1
pause 1
y&z
hold 0
-1
legend('y-function','z-function') -3
-4 -3 -2 -1 0
-π ≤ Θ ≤ π
1 2 3
15
4
x = - pi : 0.1 : pi ; plot
y = sin ( x ) ;
z = tan(sin(x)) - sin(tan(x));
plot (x , y , ' ro- ')
xlabel(' -\pi \leq \Theta \leq \pi ')
Plot of two carves
1
2
0.6
-2
0.2 -4
-0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4
-π ≤ Θ ≤ π
16
8
stem
A two-dimensional stem plot displays data as lines extending
from a baseline along the x-axis.
stem(Y) plots the data sequence Y as stems that extend from equally
spaced and automatically generated values along the x-axis. When Y
is a matrix, stem plots all elements in a row against the same x value.
stem(X,Y) plots X versus the columns of Y. X and Y are vectors or
matrices of the same size. Additionally, X can be a row or a column
vector and Y a matrix with length(X) rows.
stem(...,'fill') specifies whether to color the circle at the end of the
stem.
stem(...,LineSpec) specifies the line style, marker symbol, and color
for the stem and top marker (the base line is not affected).
17
x = - pi : 0.1 : pi ; stem 1
Plot of two carves
0.8
y = sin ( x ) ; 0.6
0.4
-0.4
-0.6
-1
-4 -3 -2 -1 0 1 2 3 4
pause 1
y&z
hold 0
-1
legend('y-function','z-function') -3
-4 -3 -2 -1 0
-π ≤ Θ ≤ π
1 2 3
18
4
9
fill
fills the 2-D polygon
fill(X,Y,C) creates filled polygons from the data in X and Y with
vertex color specified by C.C C is a vector or matrix used as an
index into the colormap. If C is a row vector, length(C) must
equal size(X,2) and size(Y,2); if C is a column vector, length(C)
must equal size(X,1) and size(Y,1). If necessary, fill closes the
polygon by connecting the last vertex to the first.
fill(X,Y,ColorSpec) fills two-dimensional polygons specified by X
and Y with the color specified
p byy ColorSpec.
p
fill(X1,Y1,C1,X2,Y2,C2,...) specifies multiple two-dimensional
filled areas.
19
x = - pi : 0.1 : pi ; fill 1
Plot of two carves
y = sin ( x ) ; 0.8
0.6
0.4
-0.4
-0.8
-1
pause 1
y&z
hold 0
-1
legend('y-function','z-function') -3
-4 -3 -2 -1 0
-π ≤ Θ ≤ π
1 2 3
20
4
10
x = - pi : 0.1 : pi ; stairs 1
Plot of two carves
y = sin ( x ) ; 0.8
0.6
0.4
y&z
0
-0.4
-0.8
-1
pause 1
y&z
hold 0
-1
legend('y-function','z-function') -3
-4 -3 -2 -1 0
-π ≤ Θ ≤ π
1 2 3
21
4
Function Plots
y Function plots allow you to use a function as input to a plot
command, instead of a set of ordered pairs of x-y values
y fplot('sin(x)',[‐2*pi,2*pi])
22
11
Plots with Error Bars
errorbar(X,Y,E) plots X versus Y with symmetric error bars 2*E(i)
long.
X Y,
X, Y E must be the same size.
size When they are vectors,
vectors each error
bar is a distance of E(i) above and below the point defined by
(X(i),Y(i)). When they are matrices, each error bar is a distance
of E(i,j) above and below the point defined by (X(i,j),Y(i,j)).
errorbar(X,Y,L,U) plots X versus Y with error bars L(i)+U(i) long
specifying the lower and upper error bars.
X Y,
X, Y L,
L and U must be the same size.
size When they are vectors,
vectors
each error bar is a distance of L(i) below and U(i) above the point
defined by (X(i),Y(i)). When they are matrices, each error bar is a
distance of L(i,j) below and U(i,j) above the point defined by
(X(i,j),Y(i,j)).
23
x=linspace(1,10,20); 12
y=10./x;
10 / 10
er=randn(20,1);
8
errorbar(x,y,er);
6
-2
0 2 4 6 8 10 12
‘er
er’’ is the array containing the error on the data. The error bar is drawn from ‘y
‘y--er
er’’ to ‘‘y+er
y+er’’
24
12
plot3
¾ The plot3 function displays a three-dimensional plot of a set
of data points.
¾ plot3(X1,Y1,Z1,...), where X1, Y1, Z1 are vectors or
matrices, plots one or more lines in three-dimensional space
through the points whose coordinates are the elements of
X1, Y1, and Z1.
¾ plot3(X1,Y1,Z1,LineSpec,...) creates and displays all lines
defined by the Xn,Yn,Zn,LineSpec
Xn Yn Zn LineSpec quads,
quads where LineSpec
is a line specification that determines line style, marker
symbol, and color of the plotted lines.
25
plot3
Example
Plot a three-dimensional helix.
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t) 35
30
25
20
15
10
0
1
0.5 1
0.5
0
0
-0.5
-0.5
-1 -1
26
13
plot3
Example
Plot a three-dimensional helix.
t = 0:pi/50:10*pi; 35
plot3(sin(t),cos(t),t)
30
25
grid on 20
15
axis square 10
0
1
0.5 1
0.5
0
0
-0.5
-0.5
-1 -1
27
stem3
Example
10
T=0:0.1:10; 0
1
S=0.1+i;
05
0.5 1
0.5
0
0
-0.5
-0.5
Y=exp(-S*T); -1 -1
stem3(real(Y),imag(Y),T,'fill');
28
14
loglog
loglog(Y) plots the columns of Y versus their index if Y contains
real numbers. If Y contains complex numbers, loglog(Y)
and loglog(real(Y),imag(Y))
loglog(real(Y) imag(Y)) are equivalent.
equivalent loglog ignores
the imaginary component in all other uses of this function.
loglog(X1,Y1,...) plots all Xn versus Yn pairs. If only Xn or Yn
is a matrix, loglog plots the vector argument versus the
rows or columns of the matrix, depending on whether the
vector's row or column dimension matches the matrix.
29
loglog
Example
Create a simple loglog plot with square markers.
45
10
40
10
x = logspace(-1,2); 35
10
loglog(x,exp(x),'-s') 30
10
25
grid on 10
20
10
15
10
10
10
default
5
10
50 points 0
10
-1 0 1 2
10 10 10 10
30
15
linspace & logspace
linspace (X1, X2) generates a row vector of (100) linearly equally
spacedd points
i between
b X11 andd X2.
2
linspace (X1, X2, N) generates N points between X1 and X2.
For N < 2, linspace returns X2.
31
semilogx , semilogy
semilogx and semilogy plot data as logarithmic scales for the x- and y-axis,
respectively, logarithmic.
semilogx(Y) creates
t a plot
l t using
i a base
b 10 logarithmic
l ith i scale
l for
f the
th x-axis
i and
d
a linear scale for the y-axis. It plots the columns of Y versus their index if Y
contains real numbers. semilogx(Y) is equivalent to semilogx(real(Y),
imag(Y)) if Y contains complex numbers. semilogx ignores the imaginary
component in all other uses of this function.
16
semilogx , semilogy
Example
C
Create a simple
i l semilogy
il plot.
l
3
10
x = 0:.1:5;
2
10
semilogy(x,10.^x)
grid on 1
10
0
10
0 0.5 1 1.5 2 2.5 3
33
plotyy
Create graphs with y-axes on both left and right side
plotyy(X1,Y1,X2,Y2)
plotyy(X1 Y1 X2 Y2) plots X1 versus Y1 with y-axis labeling
on the left and plots X2 versus Y2 with y-axis labeling
on the right.
plotyy(X1,Y1,X2,Y2,function) uses the specified plotting
function to produce the graph. 'function' can be plot,
stem , semilogx, semilogy, loglog
plotyy(X1,Y1,X2,Y2,'function1','function2') uses
function1(X1,Y1) to plot the data for the left axis and
function2(X2,Y2) to plot the data for the right axis.
34
17
plotyy
Example
This example graphs two mathematical functions using plot
as the plotting function. The two y-axes enable you to display
both sets of data on one graph even though relative values of
the data are quite different.
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
plotyy(x,y1,x,y2,'plot')
35
plotyy
200 0.8
150 0.6
100 0.4
50 0.2
0 0
-50 -0.2
-100 -0.4
-150 -0.6
-200 -0.8
0 2 4 6 8 10 12 14 16 18 20
36
18
plotyy
You can make indications on the plot, like title and labels and
so on, make the following changes to previous example;
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x , y1 , x , y2 , ' plot ' );
xlabel('
l b l(' Zero
Z to
t 20 \musec.
\ ')
title(' Labeling plotyy ')
set(H1, ' LineStyle ', ' - - ')
set(H2, ' LineStyle ', ' : ' )
37
plotyy
Labeling plotyy
200 0.8
150 0.6
100 0.4
50 0.2
0 0
-50 -0.2
-100 -0.4
-150 -0.6
-200 -0.8
0 2 4 6 8 10 12 14 16 18 20
Zero to 20 µsec. 38
19
Basic Plotting
Figure Control
☺ pause, pause(n)
☺ figure, figure(n)
Multiple Plots
☺ hold on, hold off
☺ plot(X, Y, W, Z)
☺ plotyy(X, Y, W, Z)
39
polar
Polar coordinate plot.
POLAR(THETA, RHO) makes a plot using polar coordinates of
the angle THETA,
THETA in radians,
radians versus the radius RHO.
RHO
90
1
120 60
theta=linspace(0,2*pi,100);
0.8
0.6
150 30
0.4
r=1./(1+theta.^2); 0.2
180 0
polar(theta,r,’.r:’);
210 330
240 300
270
40
20
polar
Polar coordinate plot.
POLAR(THETA, RHO) makes a plot using polar coordinates of
the angle THETA,
THETA in radians,
radians versus the radius RHO.
RHO
90
0.5
120 60
t=0:.01:2*pi;
0.4
0.3
150 30
0.2
r=abs(sin(2*t).*cos(2*t)); 0.1
180 0
polar(t,r,’-r’);
210 330
240 300
270
41
subplot
subplot divides the current figure into rectangular panes that
are numbered row-wise. Each pane contains an axes.
Subsequent plots are output to the current pane
pane.
21
subplot
Example
To plot income in the top half of a figure and outgo in
the bottom half,
5.5
4.5
outgo = [2.5
[2 5 4.0
4 0 3.35
3 35 4.9];
4 9]; 3.5
3
1 1.5 2 2.5 3 3.5 4
subplot(2,1,1); plot(income) 5
4.5
2.5
1 1.5 2 2.5 3 3.5 4
43
subplot
44
22
subplot
The following illustration shows four subplot regions and
indicates the command used to create each.
45
subplot
The following combinations produce asymmetrical
arrangements of subplots.
subplot(2,2,[1 3])
subplot(2,2,2)
subplot(2,2,4)
46
23
subplot
You can also use the colon operator to specify multiple
locations if they are in sequence.
subplot(2,2,1:2)
subplot(2,2,3)
subplot(2,2,4)
47
Example
Write a script file to show the beat phenomena in waves
% Ploting Wave Profile
a1 = 1; a2 = 1.5; c1 = 2.0; c2 = 1.8;
time = 0:0.1:100;
wave1 = a1 * sin(c1*time);
wave2 = a2 * sin(c2*time);
wave3 = wave1 + wave2;
plot(time,wave1,time,wave2,time,wave3)
xlabel ('time'); ylabel ('wave elevation');
title ('Wave Profile')
48
24
Example
Wave Profile
2.5
1.5
0.5
wave elevation
-0.5
-1
-1.5
-2
-2.5
0 10 20 30 40 50 60 70 80 90 100
time
49
time = 0:0.1:100;
wave1 = a1 * sin(c1*time);
wave2 = a2 * sin(c2*time);
wave3 = wave1 + wave2;
subplot(3,1,1) % top figure
plot(time,wave1,'m'); axis([0 100 -3 3]); y
p ylabel('wave 1');
N YMAX])
25
% Display the results in three subplots
2
ve 1
wav 0
-2
0 10 20 30 40 50 60 70 80 90 100
2
wave 2
-2
0 10 20 30 40 50 60 70 80 90 100
2
waves 1&2
-2
0 10 20 30 40 50 60 70 80 90 100
time
51
Practic
Subplot (m,n,p) Multiple plots
x=0:0.1:10;
x 0:0.1:10;
y1=sin(pi*x); y2=sin(0.5*pi*x); y3=y1+y2;
z1=cos(pi*x); z2=cos(0.5*pi*x); z3=z1-z2;
subplot(3,2,1); plot(x,y1,'b');
subplot(3,2,2); plot(x,z1,'b');
subplot(3 2 3); plot(x
subplot(3,2,3); plot(x,y2,'m');
y2 'm');
subplot(3,2,4); plot(x,z2,'m');
subplot(3,2,5); plot(x,y3,'r');
subplot(3,2,6); plot(x,z3,'r');
52
26
Subplot (m,n,p) Multiple plots
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
0 2 4 6 8 10 0 2 4 6 8 10
1 1
0.5 0.5
0 0
-0.5 -0.5
-1
1 -1
1
0 2 4 6 8 10 0 2 4 6 8 10
2 2
1 1
0 0
-1 -1
-2 -2
0 2 4 6 8 10 0 2 4 6 8 10
53
Figure
Create figure window
x=0:0.1:10;
x 0:0.1:10;
y1=sin(pi*x); y2=sin(0.5*pi*x); y3=y1+y2;
z1=cos(pi*x); z2=cos(0.5*pi*x); z3=z1-z2;
figure(1); plot(x,y1,'b');
figure(2); plot(x,z1,'b');
figure(3); plot(x
plot(x,y2,'m');
y2 'm');
figure(4); plot(x,z2,'m');
figure(5); plot(x,y3,'r');
figure(6); plot(x,z3,'r');
54
27
Exercise and Homework
Use graphical method to find the root for the
following equations :
55
Answers 15
1. x=-3:0.5:4; 10
y=x.^3-1.2500*x.^2-8.2025*x+2.1131; 0
-5
plot(x,y,'--rs')
plot(x,y, rs ) -10
-15
-3 -2 -1 0 1 2 3 4
2. x=-3:0.5:4; 50
-50
y=x.^4-2.75*x.^3-19.18*x.^2-14.29*x+3.6456; -100
-150
plot(x,y,'--rs') -200
-250
-300
-3 -2 -1 0 1 2 3 4
ggrid on
9
9Roots are -2.45
2 4 -1.2
1 2 00.2
2 66.2
2
2
3. x=-3:0.5:3; 0
-2
y=cos(x)-x.^2; -4
-6
plot(x,y,'--s') -8
-10
-3 -2 -1 0 1 2 3
28
Graphics
Bitmap images can also be visualized
load earth
image(X)
colormap(map)
57
Colormaps
50 50 50 50
50 100 150 200 250 50 100 150 200 250 50 100 150 200 250 50 100 150 200 250
50 50 50 50
colormap(cool)
l ( l) colormap(pink)
l ( i k) colormap(bone)
l (b ) colormap(copper)
l ( )
50 50 50 50
29
Graphics
Bitmap images can also be visualized
load mandrill
image(X)
colormap(map)
59
A=imread('zanko.jpg')
image(A)
colormap(map)
60
30
Handling Graphics Images
• Reading/Writing Graphics Images
– imread , imwrite
– load , save ( if only the image was saved as a MAT file)
• Displaying Graphics Images
– image
– Imagesc
• Utilities
– imfinfo
– ind2rgb
61
31