Beruflich Dokumente
Kultur Dokumente
Learning Objectives
Topics
Understand the
Plotting in 3D: major
anatomy of a 3D plot
differences
Basics of
The meshgrid() function
constructing plots in
to create plaids
3D
3D mesh plots
How to choose
3D surface plots
different plot types
for best effects
shading and light sources
Contour plots
Use of color to add a 4th
dimension
Background
Plotting in 3D and 3D data visualization is where
Matlabs power really becomes apparent!
Matlab defines a number of different kinds of 3D plots
but you will probably find 3 or 4 to be the most useful:
Anatomy of a 3D Plot
There are MANY options for plotting in 3D but we will
consider only the basics:
Plotting a curve in 3D: plot3(x,y,z)
Plotting a surface, z=f(x,y), in 3D: mesh( ) & surf( )
There are also several other plotting topics that we will NOT
cover or discuss in class (but you might find interesting to
explore on your own):
ribbon plots
quiver plots (showing vectors)
volume plots
advanced colormap use
3D Line Plot
This is the simplest (and least useful) 3D plot and is
really just a generalization of the 2D function:
z-axis
z-axis
11
00
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
x=0:0.1:3.*pi;
-1-1
11
z1=sin(x);
z2=sin(2.*x);
0.5
0.5
z3=sin(3.*x);
00 0
y-axis
y1=zeros(size(x));
0
y-axis
y3=ones(size(x));
y2=y3./2;
plot3(x,y1,z1,'r',x,y2,z2,'b',x,y3,z3,'g')
grid on
xlabel('x-axis'), ylabel('y-axis'), zlabel('z-axis')
55
x-axis
x-axis
10
10
>> theta=0:0.1:10.*pi;
>> plot3(sin(theta),cos(theta),theta)
>> grid on
theta=0:0.1:10.*pi;
>> plot3(sin(theta),cos(theta),theta)
>> grid on
40
clf
turns=40.*pi;
theta=linspace(0,turns,4000);
x=cos(theta).*(turns-theta)./turns;
y=sin(theta).*(turns-theta)./turns;
z=theta./turns;
plot3(x,y,z)
grid on
text(0.5,0.5,0.75,'Here is a piece of text!');
30
20
0.5
10
0
1
0
-1
-1
0
1
0
-1
-1
3D Surface Plots
It is often desirable to plot functions of the form: z=f(x,y)
for each (x,y), we can compute a value for z
this defines a surface in 3D space
z-axis
x =-3
-2
-1
y =-3
-2
-1
z=f(x,y)
0
-5
-10
2
0
y-axis
-2
0
-2
x-axis
NOTE:
NOTE:
xx
xxvaries
variesalong
alongROWS
ROWSwhile
whileyy
yyvaries
varies
along
COLUMNS
along COLUMNS
xx =-3
-3
-3
-3
-3
-3
-3
-2
-2
-2
-2
-2
-2
-2
-1
-1
-1
-1
-1
-1
-1
0
0
0
0
0
0
0
1
1
1
1
1
1
1
2
2
2
2
2
2
2
3
3
3
3
3
3
3
yy =-3
-3
-2
-2
-1
-1
0
0
1
1
2
2
3
3
z=f(xx,yy)
-3
-2
-1
0
1
2
3
-3
-2
-1
0
1
2
3
-3
-2
-1
0
1
2
3
-3
-2
-1
0
1
2
3
-3
-2
-1
0
1
2
3
NOTE #1:
xx varies along ROWS while yy varies
along COLUMNS
>> y=-2.5:2.5;
>> [xx,yy]=meshgrid(x,y)
xx =
-3.5
-2.5
-1.5
-0.5
0.5
1.5
2.5
3.5
-3.5
-2.5
-1.5
-0.5
0.5
1.5
2.5
3.5
-3.5
-2.5
-1.5
-0.5
0.5
1.5
2.5
3.5
-3.5
-2.5
-1.5
-0.5
0.5
1.5
2.5
3.5
-3.5
-2.5
-1.5
-0.5
0.5
1.5
2.5
3.5
-3.5
-2.5
-1.5
-0.5
0.5
1.5
2.5
3.5
NOTE #2:
For any (i, j), the value in xx is the x
coordinate, while the value in yy is the
y coordinate:
xx(3,1) = -3.5
yy(3,1) = -0.5
So (x,y) = (-3.5, -0.5)
yy =
-2.5
-2.5
-2.5
-2.5
-2.5
-2.5
-2.5
-2.5
-1.5
-1.5
-1.5
-1.5
-1.5
-1.5
-1.5
-1.5
-0.5
-0.5
-0.5
-0.5
-0.5
-0.5
-0.5
-0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.5
1.5
1.5
1.5
1.5
1.5
1.5
1.5
1.5
2.5
2.5
2.5
2.5
2.5
2.5
2.5
2.5
NOTE #3:
Matlab calls these arrays "plaids"
NOTE #4:
We can use array math to efficiently
compute the z values when z=f(x,y)
x=-3:3;
y=-3:3;
[xx,yy]=meshgrid(x,y)
=
3
-2
-1
0
-3
-2
-1
0
-3
-2
-1
0
-3
-2
-1
0
-3
-2
-1
0
-3
-2
-1
0
-3
-2
-1
0
yy =
-3
-3
-3
-3
-2
-2
-2
-2
-1
-1
-1
-1
0
0
0
0
1
1
1
1
2
2
2
2
3
3
3
3
>> zz=xx.^2 + yy.^2;
>> mesh(xx,yy,zz)
20
1
1
1
1
1
1
1
2
2
2
2
2
2
2
3
3
3
3
3
3
3
-3
-2
-1
0
1
2
3
-3
-2
-1
0
1
2
3
-3
-2
-1
0
1
2
3
15
10
5
0
4
2
0
-2
-4
NOTE:
Matlab's array instructions make this a
deceptively simple calculation for all
values at once
-4
-2
Calculation of f(x,y)
20
15
10
5
0
NOTE:
It can be very tricky to keep track of
the row and column indices in this kind
of calculation (it appears that the
text in the book is incorrect)
2
0
-2
-2
20
15
10
5
0
2
NOTE:
It can be very tricky to keep track of
the row and column indices in this kind
of calculation
0
-2
-2
[X,Y,Z] = sphere(12);
subplot(1,2,1);
mesh(X,Y,Z), title('Figure 26.5a: Opaque');
hidden on;
axis square off;
subplot(1,2,2);
mesh(X,Y,Z),title('Figure 26.5b: Transparent');
hidden off;
axis square off;
>> [x,y,z]=peaks(30);
>> mesh(x,y,z)
5
>> xlabel('x-axis')
>> ylabel('y-axis')
>> zlabel('z-axis')
z-axis
0
-5
2
Suggestion:
Try using hidden off and
hidden on to see what happens.
0
y-axis
-2
-2
0
x-axis
>> ylabel('y-axis')
>> zlabel('z-axis')
Hint::
If you just finished the previous
example, you need only type in the
new meshc( ) command.
z-axis
>> xlabel('x-axis')
0
-5
-10
2
0
y-axis
-2
-2
0
x-axis
>> [x,y,z]=peaks(30);
>> meshz(x,y,z)
>> ylabel('y-axis')
>> zlabel('z-axis')
-5
2
0
-2
-2
>> [x,y,z]=peaks(30);
>> waterfall(x,y,z)
>> xlabel('x-axis')
>> ylabel('y-axis')
z-axis
>> zlabel('z-axis')
-5
2
0
y-axis
-2
-2
0
x-axis
>> [x,y,z]=peaks(30);
z-axis
>> surf(x,y,z)
>> ylabel('y-axis')
>> zlabel('z-axis')
-5
2
0
y-axis
-2
-2
0
x-axis
>> [x,y,z]=peaks(30);
>> surf(x,y,z)
5
z-axis
>> ylabel('y-axis')
>> zlabel('z-axis')
-5
2
0
y-axis
-2
-2
0
x-axis
surfc acts much like meshc with a contour plot drawn below the
surface
shading interp interpolates color over each facet
>> [x,y,z]=peaks(30);
>> surfc(x,y,z)
>> shading interp
>> axis tight
>> xlabel('x-axis')
>> ylabel('y-axis')
>> zlabel('z-axis')
NOTE:
shading interp can take time
to execute and the figure may
cause plotting problems
viewpoint
elev
x
azimuth
view(-45,60)
Adding a Colorbar
You can use the colorbar command to add a color bar
that defines the color used in the plot. Use help
colorbar to find out what the other options are
NOTE:
You should check out the instructions
on how to use the Property Editor and
the tools in the Figure window to
interactively add text and labels to
various parts of this 3D graph.
JET, HSV, GRAY, HOT, COOL, BONE, COPPER, PINK, FLAG, PRISM
see help graph3d for more information and other colormaps
use colormap hsv or colormap(hsv) to change
colormap default restores the colormap to default values
use the colorbar command to display the color bar by itself or alongside a
plot (see help)
>> colorbar
50
>> colorbar
40
30
20
10
-5
0
-10
-5
2
0
-2
-2
-20
-30
2
-5
0
-2
-2
-40
-50
>> surf(x,y,z,y)
>> surf(x,y,z,C)
>> ylabel('y-axis')
>> colorbar
-5
-5
2
0
y-axis
-2
-2
Curvature
0.5
0
-2
-2
-0.5
Contour Plots
NOTE:
See textbook for
other options.
3
[x,y,z]=peaks(30);
contourf(x,y,z,10)
colorbar
xlabel('x-axis')
ylabel('y-axis')
6
4
1
1
y-axis
0
0
-1
y-axis
-1
-2
-2
-4
-2
-2
-4
-3
-3
-2
-1
0
x-axis
-3
-3
-6
-2
-1
0
x-axis
Example
[xx,yy]=meshgrid(-4.*pi:pi./5:4.*pi);
R=sqrt(xx.^2 + yy.^2)+eps; % radius
zz=sin(R)./R;
surf(xx,yy,zz)
axis tight
Question:
How could you handle a
situation when the data
are not defined on a
regular grid (when the
points are unevenly
spaced)?
Incentives
Here is a sphere with 2 lights added and the shading
adjusted to show the lights (gouraud shading). See if
you can create similar figures!
Now
Nowyou
youreally
reallydon't
don'thave
haveany
anyreason
reason
why
you
can't
produce
outstanding
why you can't produce outstanding
graphics
graphicsfor
forall
allyour
yourreports
reportsand
and
projects!
projects!
Problem Solving
Summary
Review questions
Action Items
Review the lecture
Work through the examples
See if you can figure out how to use color