Lecture 04: 2D & 3D Graphs: - Line Graphs - Bar-Type Graphs - Area Graphs - Mesh Graphs - Surf Graphs - Other Graphs
Lecture 04: 2D & 3D Graphs: - Line Graphs - Bar-Type Graphs - Area Graphs - Mesh Graphs - Surf Graphs - Other Graphs
University
gtext('y1') 0.5
y1 & y2
grid on y1
0
-0.5
0 2 4 6 8
t
1. 2D Line Graphs: Plot
Axis & Legend
axis([xmin xmax ymin ymax]) % sets scaling for the
x- and y-axes
axis([xmin xmax ymin ymax]) % sets scaling for the
x- and y-axes
axis equal % sets tick mark increments on the axes
are equal in size
axis square % makes the current axis box square
axis on/off % turns on(off) axis labeling
legend('str1','str2') % display legend
1. 2D Line Graphs: Plot
% For example
t= 0:0.02:8;
y1= exp(-t).*sin(t);
y2= exp(-0.5*t).*cos(t);
plot(t,y1,'k',t,y2,'r:','linewidth',2)
xlabel('t'), ylabel('y1 & y2')
legend('y1','y2','Location','NorthEast')
1
axis([0 8 -0.5 1]) y1
y2
0.5
y1 & y2
-0.5
0 2 4 6 8
t
1. 2D Line Graphs: Plot
Example 1: Draw the graph of the following parametric
equation:
x=cos3(t), y= sin3(t) (0t2)
1
t= linspace(0,2*pi,100);
0.5
x= cos(t).^3;
y= sin(t).^3; 0
y
plot(x,y,'k','LineWidth',2)
-0.5
xlabel('x'), ylabel('y')
axis equal -1
-1 0 1
x
1. 2D Line Graphs: Plot
Discountinous Graph
Use inf to draw discontinuity graphs
For example
x: -2 -1 0 1 2
y: 2 2 inf 4 4
5
x= [-2 -1 0 1 2]; 4
y= [2 2 inf 4 4];
plot(x,y,'linewidth',2) 3
axis([-3 3 1 5]) 2
1
-3 -2 -1 0 1 2 3
1. 2D Line Graphs: Plot
Adding Graphs
hold on/off % holds the current plot and all axis
properties / returns the default mode
x= 0:0.01:12;
y= x.*exp(-x);
0.5
0.4
plot(x,y,'linewidth',2) 0.3
hold on 0.1
plot(x(1),y(1),'x',x(end),y(end),' 0
o','linewidth',2) -0.1
0 2 4 6 8 10 12
hold off
1. 2D Line Graphs: Plot
Creating Axes
subplot divides the current figure into an mn grid
and creates axes in the position specified by p.
subplot(m,n,p) % create axes in tiled positions, where
p is the position in mn axes
subplot mnp % same as the above
position
1 2
3 4
5 6
1. 2D Line Graphs: Plot
Example 2: Draw the graph of the following equations
on the 12 tiled axes (a) m= 0.65, (b) m= 1.4:
x= (m-1)cos(t)+cos((m-1)t)
y= (m-1)sin(t)-sin((m-1)t) (0t40)
(a) m= 0.65 (b) m=1.4
t= 0:0.02:40*pi; subplot 122
fx= @(m) (m-1)*cos(t)+cos((m-1)*t); m= 1.4;
fy= @(m) (m-1)*sin(t)-sin((m-1)*t); plot(fx(m),fy(m),'b','linewidth',1)
axis equal off
subplot 121
m= 0.65;
plot(fx(m),fy(m),'k','linewidth',1)
axis equal off
1. 2D Line Graphs: Plot
Handling Windows
figure(n) % makes n the current figure
close(n) % closes the window with handle n
clf % clear current figure
cla % clear current axis
1. 2D Line Graphs: Log
Log Graph
semilogx(x,y) % plots semi-log scale figure for the
X-axis
semilogy(x,y) % plots semi-log scale figure for the
Y-axis
loglogx(x,y) % plots semi-log scale figure for both
the X- and Y-axis
1. 2D Line Graphs: Log
For example, define x as a vector equally spaced
numbers on the interval [0, 20] and calculate y= exx5.
Then, draw x and y in a semilogy plot.
Note that y has a large value.
1020
x= 0:0.02:20;
y= exp(x).*x.^5; 1010
semilogy(x,y,'b','linewidth',1)
xlabel('x'), ylabel('y= e^xx^5') 100
grid on
10-10
0 5 10 15 20
x
1. 2D Line Graphs: Log
For example, define as a vector logarithmically
spaced numbers on the interval (10-2102) and
calculate G(). Then plot and G in a semilogx plot.
1
G ( )
(9 2 ) 2 4 2
x= 0:0.02:20; 0.2
w= logspace(-2,2,200); 0.15
semilogx(w,Gw,'b','linewidth',1) 0.05
xlabel('\omega'),ylabel('G(\omega)')
grid on 0
10-2 10-1 100 101 10 2
1. 2D Line Graphs: Contour
Contour Graph
contour(Z) % plots a contour of Z
contour(X,Y,Z) % plots a contour of Z at X and Z
contour(X,Y,Z,n) % plots n contour lines of Z at X
and Z
contour(X,Y,Z,v) % plots a contour of Z at X and Z
at the level v
C= contour(…); % plots a contour of Z at X and Z
clabel(C) % plots elevation labels using the
contour matrix C
1. 2D Line Graphs: Contour
For example, draw the contour of y= x2-y2 (-2x,y2)
x= -2:0.2:2; x= -2:0.2:2;
y= x; [X,Y]= meshgrid(x);
[X,Y]= meshgrid(x,y); Z= X.^2-Y.^2;
Z= X.^2-Y.^2; v= [-2 -0.5 0.01 1.5 3];
contour(X,Y,Z,10,'k') C= contour(X,Y,Z,v,'k');
clabel(C)
2 2 -2
-0.5
1.5 1.5
0.01 0.01
1.5 1.5
1 1
3 3
0.5 0.5
0 0
-0.5 -0.5
3
-1 -1
-0.5
-1.5 -1.5
-2
-2 -2
-2 -1 0 1 2 -2 -1 0 1 2
2. 2D Bar-type Graphs: Bar
Bar Graph
bar(y) % plots vertical bar graph
bar(x,y) % plots vertical bar graph
bar(x,y,'stacked') % plots a vertical stacked bar
barh(y) % plots horizontal bar graph
barh(x,y) % plots horizontal bar graph
barh(x,y,'stacked') % plots
horizontal stacked bar
2. 2D Bar-type Graphs: Bar
% For example
rand('seed', 126);
Y= round(rand(5,4)*5);
subplot 121
bar(Y,'grouped')
subplot 122
barh(Y,'stacked')
2. 2D Bar-type Graphs: Rectangle
Rectangle & Boxplot
rectangle('Position', [x,y,wh,ht]) % adds a rectangle
at the specified position
h= rectangle('Position', [x,y,wh,ht]) % adds a
rectangle and returns its handle
boxplot(z) % displays box plots of multiple data
samples
2. 2D Bar-type Graphs: Rectangle
0 180 0
-1 210 330
240 300
-2 270
-2 -1 0 1 2
4. Other 2D Graphs:
Example 6: Draw the quiver graph of the equation
together with its contour
z= xexp(-x2-y2) (-2x,y2) where x= y= 0.2.
dx= 0.2; dy= 0.2; 2
x= -2:dx:2; y= -2:dy:2;
[X,Y]= meshgrid(x,y); 1
Z= X.*exp(-X.^2-Y.^2);
0
[PX,PY]= gradient(Z,dx,dy);
quiver(x,y,PX,PY,1,'k'), hold on -1
contour(X,Y,Z,'k')
axis equal -2
-2 -1 0 1 2
axis([-2 2 -2 2]), hold off
4. Other 2D Graphs: Patch
Patch
patch(x,y,c) % adds the filled 2D polygon defined by
vectors x and y with color c
patch('vertices',verts,'faces',faces,'facecolor','c') %
creates the patch by specifying the faces, vertices and
facecolor properties.
4. Other 2D Graphs: Patch
% For example 10
x= [1 4 1]; y= [2 2 6];
8
patch(x,y,'g')
6
axis([0 10 0 10]) 3
0
0 2 4 6 8 10
% For example
verts= [1 2;4 2;1 6]; 8
faces= [1 2 3]; 6
patch('vertices',verts,'faces',face
5
s,'facecolor','g') 3
axis([0 10 0 10]) 1
0
0 1 2 3 4 5 6
4. Other 2D Graphs: Patch
% For example
verts= [1 2;4 2;1 6];
faces= [1 2 3]; rgb= [0.5 0.1 0.5];
patch('vertices',verts,'faces',faces,'facecolor',rgb)
axis([0 10 0 10])
8
0
0 1 2 3 4 5 6
5. 3D Line Graphs: Plot3
Plot3 Graph
plot3(x,y,z) % plots a 3D line graph with x, y and z of
the same length
plot3(x,y,z,Specifier,PropertyName,PropertyValue)
% plots a 3D line graph with options
h= plot3(…) % plots a 3D line graph and returns its
handle
Axis
axis([xmin xmax ymin ymax zmin zmax]) % sets the
scaling for the x-, y- and z-axes
5. 3D Line Graphs: Plot3
% For example
t= linspace(-pi,pi,200)+eps;
x= sin(t); y= cos(t);
z= zeros(size(x));
plot3(x,y,z,'r','linewidth',2)
hold on
Nero's
z= abs(sin(10*t)./(10*t)); crown
plot3(x,y,z,'b','linewidth',3)
axis off
hold off
5. 3D Line Graphs: Plot3
Meshgrid
X is a 2D grid coordinate matrix where each row is a
copy of x, and Y is a matrix where each column is a
copy of y.
[X,Y]= meshgrid(x,y) % transforms the domain
specified by vectors x and y into arrays X and Y
[X,Y]= meshgrid(x) % same as the above(y= x)
z y
x x
6. 3D Mesh Graphs: Mesh
>> x= 1:3; y= -1:1;
>> [X,Y]= meshgrid(x,y)
>> Z= X.^2+Y.^2
X=1 2 3 Y= -1 -1 -1
1 2 3 0 0 0
1 2 3 1 1 1 10
z
5
Z= 2 5 10
1 4 9 0
1
2 5 10 0
3
y 2
-1 1
x
6. 3D Mesh Graphs: Mesh
Mesh
Draws a 3-D mesh surface
mesh(Z) % plots 3D mesh surface of Z
mesh(X,Y,Z) % plots 3D mesh surface of Z at X and
Y(color is proportional to mesh height)
meshc(X,Y,Z) % plots 3D mesh surface with 2D
contour of Z at X and Y
meshz(X,Y,Z) % plots 3D mesh surface of Z at X and
Y with curtain
6. 3D Mesh Graphs: Mesh
0.5
% For example 0
x= -2:0.2:2; 0.5
2
[X,Y]= meshgrid(x); 0 0
2
-2 -2
F= X.*exp(-X.^2-Y.^2);
subplot 311 0.5
mesh(X,Y,F) 0
0.5
2 2
subplot 312 0
-2 -2
0
meshc(X,Y,F)
0.5
subplot 313 0
meshz(X,Y,F) 0.5
2
0 2
0
-2 -2
6. 3D Mesh Graphs: Mesh
Colormap
colormap % displays the color look-up table of the
current window
map= colormap % retrieves the current colormap,
which is a 643 matrix whose column means RGB
color intensities in the range of 0~1.
colormap(map) % sets the current figure's colormap
to map
6. 3D Mesh Graphs: Mesh
Color Intensities
>> colormap
0 0 0.5625
0 0 0.6250
0 0 0.6875
…
0.8125 1.0000 0.1875 [0 0 0] is black,
0.8750 1.0000 0.1250 [1 1 1] is white,
0.9375 1.0000 0.0625 [1 0 0] is pure red,
R G B [.5 .5 .5] is gray
6. 3D Mesh Graphs: Mesh
Built-in Colormaps
autumn varies smoothly from red,
through orange, to yellow
bone is a grayscale colormap with
a higher value for the blue
component
cool consists of colors that are
shades of cyan and magenta. It
varies smoothly from cyan to
magenta
copper varies smoothly from
black to bright copper
set to the minimum of Z set to the maximum of Z
6. 3D Mesh Graphs: Mesh
% For example
x= -2:0.2:2; hsv cool
[X,Y]= meshgrid(x); 0.5 0.5
F= X.*exp(-X.^2-Y.^2); 0 0
0.5 0.5
subplot 311 2
0
-2 -2
0
2 2
0
-2 -2
0
2
mesh(X,Y,F)
0.5 0.5
subplot 312 0 0
0.5 0.5
meshc(X,Y,F) 2
0 0
2
2
0
-2 -2
0
2
-2 -2
meshz(X,Y,F) 0
0
0.5
colormap cool 0.5
2
0 0
2
2
0
-2 -2
0
2
-2 -2
6. 3D Mesh Graphs: Mesh
Example 1: Draw the graph of Torus equation:
x=cos(u)(R+rcos(v))
y=sin(u)(R+rcos(v))
z=rsin(v) (0 u,v 2)
where R is the distance from the
center (major radius) and r is the minor radius (tube)
6. 3D Mesh Graphs: Mesh
% For example
R= 1; r= 0.5;
u= linspace(0,2*pi,30);
[U,V]= meshgrid(u);
X= cos(U).*(R+r*cos(V));
Y= sin(U).*(R+r*cos(V));
Z= r*sin(V);
mesh(X,Y,Z)
axis equal;
axis off
view(55,40)
6. 3D Mesh Graphs: Trimesh
Trimesh
TRI= delaunay(x,y) % returns a set of triangles that
define one such triangle and contain indices into
vectors x and y
trimesh(TRI,X,Y,Z) % displays a trianglular mesh
plot defined in the M-by-3 face matrix TRI as a mesh.
% For example
x= 1:20;
[X,Y] = meshgrid(x);
TRI= delaunay(X,Y);
Z= peaks(20);
trimesh(TRI,X,Y,Z), axis off
6. 3D Mesh Graphs: Stem3
Stem3 Graph
stem3(Z) % displays a 3D stem plot of Z
stem3(X,Y,Z) % displays a 3D stem plot of Z at X
and Y
stem3(X,Y,Z,'filled') % displays a 3D stem plot of Z
at X and Y with filled markers
% For example 1
X= linspace(0,2*pi,50); 0.5
Z= cos(X); 0
-0.5
stem3(Z) -1
2
60
1 40
20
0 0
6. 3D Mesh Graphs: Stem3
Example 2: Draw the 3D stem graph of the
following equations:
x= cos(t), y= sin(t),
z= |sin(4t)/4t| (- t )
7. 3D Surf Graphs: Surf
Surf Graph
surf(X,Y,Z) % plots a 3D colored surface of Z at X
and Y
surfc(X,Y,Z) % displays a 3D colored
surfacesurf/contour plot
surfl(X,Y,Z) % plots a 3D colored surface with
lighting
shading interp/flat/faceted % controls the color
shading of surface(faceted: default)
7. 3D Surf Graphs: Surf
For example, draw the mesh, surf, surfl graphs of the
equation on the 31 tiled axes
z= -5xyexp(-x2-y2) (-4 x, y 4)
x= -4:0.2:4;
[X,Y]= meshgrid(x);
Z= -5*X.*Y.*exp(-X.^2-Y.^2);
subplot 311
mesh(X,Y,Z)
subplot 312
surf(X,Y,Z)
subplot 313
surfl(X,Y,Z)
7. 3D Surf Graphs: Surf
Example 4: Draw the surf, surfl graphs of the
following equation on the 13 tiled axes with
interp/flat/faceted shadings
y= -5xyexp(-x2-y2) (-4 x, y 4)
7. 3D Surf Graphs: Trisurf
Trisurf & Cylinder
trisurf(TRI,X,Y,Z) % plots a 3D colored triangular
surface of Z at X and Y
[X,Y,Z]= cylinder(r) % generates the unit cylinder
based on the generator curve in the vector r
[X,Y,Z]= cylinder(r) % generates the unit cylinder
based on the generator curve in the vector r with n
points around the circumference
7. 3D Surf Graphs: Trisurf
For example, draw the trisurf graph of the following
equation
y= -5xyexp(-x2-y2) (-4 x, y 4)
x= -4:0.25:4;
[X,Y]= meshgrid(x);
Z= -5*X.*Y.*exp(-X.^2-Y.^2);
TRI= delaunay(X,Y);
trisurf(TRI,X,Y,Z)
axis off
8. Other 3D Graphs: Cylinder
For example, draw the mesh, surf cylinder graphs of
the following equation on the 21 tiled axes
r= cos(t)+1 (0 t 2)
t= linspace(0,2*pi,30);
r= cos(t)+1;
[X,Y,Z]= cylinder(r);
subplot 121
mesh(X,Y,Z), axis([-2 2 -2 2 0 1])
subplot 122
surf(X,Y,Z),axis([-2 2 -2 2 0 1])
8. Other 3D Graphs: Sphere
Sphere & Other 3D Graphs
sphere(n) % produces a unit sphere
[X,Y,Z]= sphere(n) % generates three (N+1)(N+1)
matrices for a unit sphere
fill3(x,y,z,'c') % fills the 3D polygon defined by
vectors x, y and z with color c
bar3(Z) % displays 3D bar graph
pie3(Z) % displays 3D pie graph
8. Other 3D Graphs: Sphere
% For example
x0= 1; y0= 1; z0= 1;
r= 2;
[X,Y,Z]= sphere(20);
X= r*X+x0;
Y= r*Y+y0;
Z= r*Z+z0;
surf(X,Y,Z)
axis equal
colormap summer
9. Other Commands
Other Commands
view(az,el) % specifies 3D graph viewpoint. az is the
azimuth and el is the elevation(degree unit)
colorbar % displays color bar (color scale)
whitebg % changes axes background color(toggle
type)
alpha(v) % sets an alpha property to v of 0~1 (e.g., 0-
clear, 1- opaque)
axis([xmin xmax ymin ymax zmin zmax]) % sets the
scaling for the x-, y- and z-axes
9. Other Commands
% For example
x= -2:0.2:2;
[X,Y]= meshgrid(x); 0.4
F= X.*exp(-X.^2-Y.^2); 0.5
0.2
surf(X,Y,F)
z=f(x,y)
0 0
xlabel('x'); ylabel('y');
-0.5 -0.2
zlabel('z=f(x,y)'); 2
2
0
colorbar y -2 -2 x
0 -0.4
colormap jet
9. Other Commands
% For example
x= -5:1:5;
[X,Y]= meshgrid(x);
Z= X.^2-Y.^2;
subplot 121
surf(X,Y,Z)
view(-23,8), axis off
subplot 122
surf(X,Y,Z) opaque= 0.5
view(-23,8),alpha(0.5)
axis off
colormap jet
Q&A