11. Three-Dimensional Graphics
11.1 Generating three-dimensional bar graphs
11.2 Plotting in three dimensions
11.5 “Surfing” the “web”
11.7 Generating contours
11.8 Checking your understanding of meshgrid-based graphing
11.9 Generating rectangular solids
11.10 Generating spheres and cylinders
11.11 Generating ellipsoids
11.12 Practicing three-dimensional graphics
Code 11.1.1
m = [];
for j = 2:4
clear n x
[n x] = hist(randn(1,10^j),10);
subplot(3,2,((j-1)*2)-1)
bar(n)
m = [m;n];
end
subplot(3,2,[2 4 6])
bar3(m')
Output 11.1.1

Code 11.2.1
figure(3)
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t)
axis square;
grid on
box on
xlabel('sin(time)','rotation',0);
ylabel('cos(time)','rotation',-0);
zlabel('time');
title('Upward Spiral');
Output 11.2.1

Code 11.3.1
figure(4)
[X,Y] = meshgrid(linspace(-2,2,41));
Z = X.*exp(-X.^2 - Y.^2);
plot3(X,Y,Z)
grid on
Output 11.3.1

Code 11.4.1
figure(5)
mesh(X,Y,Z)
box on
Output 11.4.1

Code 11.4.2
figure(6)
mesh(X,Y,Z)
hold on
plot3(X,Y,Z,'k.')
box on
Output 11.4.2

Code 11.5.1
figure(7)
surf(X,Y,Z)
title('Surf''s Up!')
box on
get(gca)
Output 11.5.1

ActivePositionProperty = outerposition
ALim = [0.1 10]
ALimMode = auto
AmbientLightColor = [1 1 1]
Box = on
CameraPosition = [-18.2628 -23.8006 4.33013]
CameraPositionMode = auto
CameraTarget = [0 0 0]
CameraTargetMode = auto
CameraUpVector = [0 0 1]
CameraUpVectorMode = auto
CameraViewAngle = [10.3396]
CameraViewAngleMode = auto
CLim = [-0.421834 0.421834]
CLimMode = auto
Color = [1 1 1]
CurrentPoint = [ (2 by 3) double array]
ColorOrder = [ (7 by 3) double array]
DataAspectRatio = [4 4 1]
DataAspectRatioMode = auto
DrawMode = normal
FontAngle = normal
FontName = Helvetica
FontSize = [10]
FontUnits = points
FontWeight = normal
GridLineStyle = :
Layer = bottom
LineStyleOrder = -
LineWidth = [0.5]
MinorGridLineStyle = :
NextPlot = replace
OuterPosition = [0 0 1 1]
PlotBoxAspectRatio = [1 1 1]
PlotBoxAspectRatioMode = auto
Projection = orthographic
Position = [0.13 0.11 0.775 0.815]
TickLength = [0.01 0.025]
TickDir = out
TickDirMode = auto
TightInset = [0.0571429 0.05 0.0303571 0.0571429]
Title = [1168.01]
Units = normalized
View = [-37.5 30]
XColor = [0 0 0]
XDir = normal
XGrid = on
XLabel = [1169.01]
XAxisLocation = bottom
XLim = [-2 2]
XLimMode = auto
XMinorGrid = off
XMinorTick = off
XScale = linear
XTick = [-2 -1 0 1 2]
XTickLabel =
-2
-1
0
1
2
XTickLabelMode = auto
XTickMode = auto
YColor = [0 0 0]
YDir = normal
YGrid = on
YLabel = [1170.01]
YAxisLocation = left
YLim = [-2 2]
YLimMode = auto
YMinorGrid = off
YMinorTick = off
YScale = linear
YTick = [-2 -1 0 1 2]
YTickLabel =
-2
-1
0
1
2
YTickLabelMode = auto
YTickMode = auto
ZColor = [0 0 0]
ZDir = normal
ZGrid = on
ZLabel = [1171.01]
ZLim = [-0.5 0.5]
ZLimMode = auto
ZMinorGrid = off
ZMinorTick = off
ZScale = linear
ZTick = [-0.5 0 0.5]
ZTickLabel =
-0.5
0
0.5
ZTickLabelMode = auto
ZTickMode = auto
BeingDeleted = off
ButtonDownFcn =
Children = [1167.01]
Clipping = on
CreateFcn =
DeleteFcn =
BusyAction = queue
HandleVisibility = on
HitTest = on
Interruptible = on
Parent = [7]
Selected = off
SelectionHighlight = on
Tag =
Type = axes
UIContextMenu = []
UserData = []
Visible = on
Code 11.6.1
help view
Output 11.6.1
VIEW 3-D graph viewpoint specification.
VIEW(AZ,EL) and VIEW([AZ,EL]) set the angle of the view from which an
observer sees the current 3-D plot. AZ is the azimuth or horizontal
rotation and EL is the vertical elevation (both in degrees). Azimuth
revolves about the z-axis, with positive values indicating counter-
clockwise rotation of the viewpoint. Positive values of elevation
correspond to moving above the object; negative values move below.
VIEW([X Y Z]) sets the view angle in Cartesian coordinates. The
magnitude of vector X,Y,Z is ignored.
Code 11.6.2
figure(8)
surf(X,Y,Z)
set(gca,'view',[0,90])
Output 11.6.2

Code 11.7.1
figure(9)
contour(X,Y,Z)
Output 11.7.1.

Code 11.8.1
[x,y] = meshgrid(1:8)
Output 11.8.1
x =
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
y =
1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
Code 11.8.2
figure(10)
z = (10*(x-mean(mean(x))).^2) + (10*(y-mean(mean(y))).^2);
for v = 1:5
if v < 5
subplot(3,2,v)
surfl(x,y,z)
else
subplot(3,2,5:6)
surfc(x,y,z)
end
zlim([0 max(max(z))+2]);
set(gca,'view',[50.5 v*76.2987]);
end
Output 11.8.2

The code below shows the contour map for the surface.
Code 11.8.3
contour(x,y,z)
Output 11.8.3

Code 11.8.4
figure(3)
[x,y] = meshgrid(1:61);
[rows columns] = size(x);
x_low_attractor = .5*mean(mean(x));
x_high_attractor = 1.5*mean(mean(x));
y_low_attractor = .5*mean(mean(y));
y_high_attractor = 1.5*mean(mean(y));
k = 5;
for r = 1:rows
for c = 1:columns
if abs(x(r,c)-x_low_attractor) <= …
abs(x(r,c)-x_high_attractor)
x_attractor = x_low_attractor;
else
x_attractor = x_high_attractor;
end
if abs(y(r,c)-y_low_attractor) <= …
abs(y(r,c)-y_high_attractor)
y_attractor = y_low_attractor;
else
y_attractor = y_high_attractor;
end
z(r,c) = (k*(x(r,c)-x_attractor).^2) + ...
(k*(y(r,c)-y_attractor).^2);
end
end
surfc(x,y,z)
Output 11.8.4

Code 11.8.3
figure(4)
contour(x,y,z)
Output 11.8.3

Code 11.9.1
function drawcube=cube(coord);
% coord = 1x3 front/bottom/left coordinates matrix
x = coord(1);
y = coord(2);
z = coord(3);
vertices_matrix = [[x y z];[x+1 y z];[x+1 y+1 z];[x y+1 z]; ...
[x y z+1];[x+1 y z+1];[x+1 y+1 z+1];[x y+1 z+1]];
faces_matrix = [[1 2 6 5];[2 3 7 6];[3 4 8 7];[4 1 5 8];...
[1 2 3 4];[5 6 7 8]];
drawcube = patch('Vertices',vertices_matrix,'Faces',faces_matrix,...
'FaceColor','g');
Code 11.9.2
cube([1 2 3])
Output 11.9.2

Code 11.10.1
figure(5)
[x y z] = sphere(24);
hold on
for j = 1:2
surf(x + j,y + j, z + j);
end
axis equal
grid on
box on
Output 11.10.1

Code 11.10.2
figure(6)
hold on
AZ = -37.5,;
EL = 30;
view(AZ,EL)
for j = 1:2
if j == 1
[x y z] = cylinder(24);
k = 1;
s = surf(x + k,y + k, z + k);
set(s,'facecolor','r');
else
k = .75;
[x y z] = cylinder(18);
s = surf(x + k,y + k, z + k);
set(s,'facecolor','b');
end
end
axis off
Output 11.10.2

Code 11.11.1
xc = 1; yc = 2; zc = 3;
xr = 1; yr = 1, zr = 3;
n_facets = 48;
[x,y,z]=ellipsoid(xc,yc,zc,xr,yr,zr,n_facets);
surf(x,y,z);
axis equal;
Output 11.11.1

Code 11.11.2
% Ellipsoid_Man_Matt_Walsh
% March_23_2006
close all
clear all
clc
figure(1)
%thorax
[x y z]=ellipsoid(2,3,7.3,1,1,3);
surf(x,y,z);
%head
hold on
[x y z]=ellipsoid(2,3,10.7,1,1,1);
surf(x,y,z);
%shoulder mass
[x y z]=ellipsoid(2,3,9,1,2,.8);
surf(x,y,z);
%right arm
[x y z]=ellipsoid(3.2,1.4,9.2,1.8,.5,.5);
surf(x,y,z);
%right forearm
[x y z]=ellipsoid(5.9,1.4,9.2,1.3,.4,.4);
surf(x,y,z);
%left forearm
[x y z]=ellipsoid(3.5,4.5,7.1,1.3,.4,.4);
surf(x,y,z);
%left arm
[x y z]=ellipsoid(2,4.5,8.1,.5,.5,1.3);
surf(x,y,z);
%right thigh
[x y z]=ellipsoid(3.33,4,5.1,1.9,.6,.6);
surf(x,y,z);
%left thigh
[x y z]=ellipsoid(3.33,2,4.7,1.9,.6,.6);
surf(x,y,z);
%bubble butt
[x y z]=ellipsoid(2,3,4.7,.8,1.5,.5);
surf(x,y,z);
%right calf
[x y z]=ellipsoid(4.7,2,3,.5,.5,1.4);
surf(x,y,z);
%left calf
[x y z]=ellipsoid(5,2.5,5.2,.5,1.6,.5);
surf(x,y,z);
%left foot
[x y z]=ellipsoid(5.4,1,5.2,1,.2,.505);
surf(x,y,z)
%right foot
[x y z]=ellipsoid(5.2,2,1.8,1,.505,.2);
surf(x,y,z);
grid on
axis on
zlim =[0 20];
shading interp;
light;
axis equal
set (gca,'view',[107,30], 'AmbientLightColor', [1 0 0] );
Output 11.11.2

Code 11.11.3
% Playing_frisbee_Robrecht_Van_Der_Wel.m
% March_23_2006
close all
clear all
clc
figure(1)
set(gcf, 'Color', [.2 .8 .8]);
title('Playing frisbee', 'FontSize', 20);
colormap(autumn);
subplot(4,2,[1:6]);
% Frisbee person
% Order is: Head, mouth/hair, eyes, nose, shoulders, torso,
% gluteus, left arm, left forearm, left hand, right arm,
% right forearm, right hand, right calf, right foot
hold on
%Head M/H Eyes Nose Shou Tors GM LA LFA LH RA RFA RH RC RF
x_1 = [-10 -9.5 -9.2 -9.2 -10 -10 -10 -10 -10 -10 ...
-8.8 -7.3 -7.2 -9.0 -8.5];
y_1 = [3 3.1 3.1 3.1 3 3 3 4.5 4.5 4.5 1.4 2.4 ...
3.9 2.5 2.5];
z_1 = [10.7 10.7 10.7 10.3 9 7.3 4.7 8.1 6.5 5 9.2 ...
9.2 9.2 1.7 .3];
x_rad_1 = [1 .2 .2 .4 1 1 .8 .5 .4 .3 1.8 .4 ...
.35 .45 .9];
y_rad_1 = [1 .5 1 .2 2 1 .9 .5 .4 .2 .5 1.3 ...
.4 .4 .3];
z_rad_1 = [1 1 .2 .2 .8 3 .5 1.3 1.3 .5 .5 .4 ...
.3 1.5 .2];
for i = 1:length(x_1)
[xpos_1 ypos_1 zpos_1]= ...
ellipsoid(x_1(i),y_1(i),z_1(i),x_rad_1(i),y_rad_1(i),z_rad_1(i));
surf(xpos_1,ypos_1,zpos_1);
end
shading interp;
light;
[xpos_1 ypos_1 zpos_1]=ellipsoid(-13.1,3.6,1.6,.9,.3,.2);
left_foot_1 = surf(xpos_1,ypos_1,zpos_1);
zdir = [0 1 0];
center = [-13.1,3.6,1.6];
rotate(left_foot_1,zdir,50,center);
[xpos_1 ypos_1 zpos_1]=ellipsoid(-10.5,3.6,3.75,.6,.5,1.3);
left_thigh_1 = surf(xpos_1,ypos_1,zpos_1);
zdir = [0 1 0];
center = [-10.5,3.6,3.75];
rotate(left_thigh_1,zdir,50,center);
[xpos_1 ypos_1 zpos_1]=ellipsoid(-12.1,3.6,2.5,.45,.4,1.5);
left_calf_1 = surf(xpos_1,ypos_1,zpos_1);
zdir = [0 1 0];
center = [-12.1,3.6,2.5];
rotate(left_calf_1,zdir,70,center);
[xpos_1 ypos_1 zpos_1]=ellipsoid(-9.4,2.6,3.8,.6,.5,1.3);
right_thigh_1 = surf(xpos_1,ypos_1,zpos_1);
zdir = [0 1 0];
center = [-9.4,2.6,3.8];
rotate(right_thigh_1,zdir,160,center);
% Catching person
% Order is: Head, hat,mouth/hair, eyes, nose, shoulders, torso,
% gluteus, left arm,
% left forearm, left hand, right arm, right forearm, right hand,
% right thigh,right calf, right foot
hold on
x_2 = [12 12 11.5 11.3 11.3 12 12 12 12 12 12 11 9.8 8.5 12 ...
12 11.4];
y_2 = [3 3 3.1 3.1 3.1 3 3 3 1.5 1.5 1.5 4.5 4.5 4.5 3.5 3.5 3.5];
z_2 = [10.7 11.5 10.5 10.7 10.3 9 7.3 4.7 8.1 6.5 5 9 9 9 3.9 ...
1.5 .1];
x_rad_2 = [1 1 .2 .2 .4 1 1 .8 .5 .4 .3 1.3 1.3 .5 .6 .45 .9];
y_rad_2 = [1 1 .5 1 .2 2 1 .9 .5 .4 .2 .5 .4 .2 .5 .4 .3];
z_rad_2 = [1 .2 1 .2 .2 .8 3 .5 1.3 1.3 .5 .5 .4 .3 1.3 1.5 .2];
for i = 1:length(x_2)
[xpos_2 ypos_2 zpos_2]= ellipsoid(x_2(i),y_2(i),z_2(i),...
x_rad_2(i), y_rad_2(i),z_rad_2(i));
surf(xpos_2,ypos_2,zpos_2);
end
[xpos_2 ypos_2 zpos_2]=ellipsoid(11.3,2.4,4,1.3,.5,.6);
left_thigh_2 = surf(xpos_2,ypos_2,zpos_2);
zdir = [0 1 0];
center = [11.6 2.3 2.4];
rotate(left_thigh_2,zdir,55,center)
[xpos_2 ypos_2 zpos_2]=ellipsoid(14,2.5,2.5,.45,.4,1.5);
left_calf_2 = surf(xpos_2,ypos_2,zpos_2);
zdir = [0 1 0];
center = [14 2.5 2.5];
rotate(left_calf_2,zdir,125,center)
[xpos_2 ypos_2 zpos_2]=ellipsoid(14.68,2.5,1.2,.9,.3,.2);
left_foot_2 = surf(xpos_2,ypos_2,zpos_2);
zdir = [0 1 0];
center = [14.68 2.5 1.2];
rotate(left_foot_2,zdir,125,center)
% Playground
[x y z]=cylinder(20,50,1);
surf(x,y,z);
shading flat;
% Frisbee
[x y z] = ellipsoid(0,1,9,1.4,1.4,.2);
surf(x,y,z);
shading flat;
grid off
axis off
xlabel('x');
ylabel('y');
zlabel('z');
axis equal
set (gca,'view',[134,14], 'AmbientLightColor', [.5,.8,.1]);
camzoom(3);
camtarget([0 0 4]);
Output 11.11.3
