
一、基本三维绘图
基本绘图步骤:
%数据准备:
x=-8:0.1:8;
y=-8:0.1:8;
[X,Y]=meshgrid(x,y);
Z=(exp(X)-exp(Y)).*sin(X-Y);
%图形窗口和绘图区选择:
figure
%绘图:
surf(X,Y,Z)
%设置视角:
view([75 25])
%设置颜色表:
colormap hsv
shading interp
%设置光照效果:
light(‘Position’,[1 0.5 0.5])
lighting gouraud
material metal
%设置坐标轴刻度和比例:
axis square
set(gca,’ZTickLabel’)
%标注图形:
Xlabel(‘x’)
Ylabel(‘y’)
colorbar
1.三维曲线图
例:三维曲线图
close all
x=-5:0.4:5;
y=5:-0.4:-5;
z=exp(-0.2*x).*cos(y);
figure
subplot(2,1,1);
plot3(x,y,z,’or’,x,y,z)
[X,Y]=meshgrid(x,y);
Z= exp(-0.2*X).*cos(Y);
subplot(2,1,2);
plot3(X,Y,Z)
2.三维曲面图
(1)矩形网络
例:矩形网络
x=-5:0.4:5;
y=5:-0.4:-5;
[X,Y]=meshgrid(x,y);
subplot(2,1,1);
plot(x,y,’o’)
subplot(2,1,2)
plot(X,Y,’o’)
(2)三维网线图
例:三维网线图
close all
clear
[X,Y]=meshgrid(-3:0.5:3);
Z=2*X.^2-3*Y.^2;
subplot(2,2,1);plot3(X,Y,Z);title(‘plot3’)
subplot(2,2,2);mesh(X,Y,Z);title(‘mesh’)
subplot(2,2,3);meshc(X,Y,Z);title(‘meshc’)
subplot(2,2,4);meshz(X,Y,Z);title(‘meshz’)
(3)三维表面图
例:三维表面图
close all
clear
[X,Y]=meshgrid(-3:0.5:3);
Z=2*X.^2-3*Y.^2;
subplot(2,2,1);mesh(X,Y,Z);title(‘mesh’)
subplot(2,2,2);surf(X,Y,Z);title(‘surf)
subplot(2,2,3);surfc(X,Y,Z);title(‘surfc’)
subplot(2,2,4);surfl(X,Y,Z);title(‘surfl’)
(4) 网格边框线设置
例:网格边框线设置
close all
clear
[X,Y]=meshgrid(-3:0.25:3);
Z=-sqrt(X..^2+3*Y.^2);
subplot(2,2,1);mesh(X,Y,Z); hidden on ; title(‘hidden on’)
subplot(2,2,2);mesh(X,Y,Z); hidden off; title(‘hidden off)
二、特殊绘图函数
1.柱状图
例:三维柱状图
clear
x=rand(3,10);
subplot(2,2,1);bar(x);title(‘bar’)
subplot(2,2,2);barh(x,’stack’);title(‘barh stack’)
subplot(2,2,3);bar3(x);title(‘bar’)
subplot(2,2,4);bar3h(x,’stack’);title(‘‘bar3h stack’’)
2.散点图
例:三维散点图
close all
clear
x=rand(1,10);
y=rand(1,10);
z=x.^2+y.^2;
scatter3(x,y,z,’ro’)
hold on
[X,Y]=meshgrid(0:0.1:1);
Z=X.^2+Y.^2;
mesh(X,Y,Z)
hidden off
3.饼状图
例:三维饼状图
x=[32 45 11 76 56];
explode=[0 0 1 0 1];
pie3(x,explode)
4.火柴杆图
例:三维火柴杆图
clear
x=rand(1,10);
y=rand(1,10);
z=x.^2+2*y;
stem3(x,y,z,’fill’)
5.向量场图
例:三维向量场图
clear
close all
[X,Y]=meshgrid(-3:0.4:3);
Z=-3*X.^2-Y.^2;
[U,V,W]=surfnorm(X,Y,Z);
quiver3(X,Y,Z,U,V,W,0.2)
hold on
surf(X,Y,Z)
6.等值线图
例:三维等值线图
clear
close all
[X,Y]=meshgrid(-3:0.01:3);
Z=X.^2+Y.^2;
contour3(X,Y,Z,20)
view([45 50])
7.简易绘图函数
例:简易三维绘图函数
close all
clear
subplot(2,2,1); ezplot(‘sin(t)’,’cos(t)’,’sin(2*t)’,[0,2*pi])
subplot(2,2,2);ezmesh(@peaks, [-5 5 -5 5])
subplot(2,2,3);ezsurf(@(x,y)(x.^2+y.^2), [-5 5 -5 5])
subplot(2,2,4);ezsurfc(@(x,y)( x.^2+y.^2), [-5 5 -5 5])
三、三维图形显示控制
1.设置坐标轴
例:设置坐标轴
close all
subplot(1,3,1)
ezsurf(@(t,s)(sin(t).*cos(s)), @(t,s)(sin(t).*sin(s)), @(t,s)(cos(t)), [0,2*pi,0,2*pi] )
axis auto; title(‘auto’)
subplot(1,3,2)
ezsurf(@(t,s)(sin(t).*cos(s)), @(t,s)(sin(t).*sin(s)), @(t,s)(cos(t)), [0,2*pi,0,2*pi] )
axis equal; title(‘equal’)
subplot(1,3,3)
ezsurf(@(t,s)(sin(t).*cos(s)), @(t,s)(sin(t).*sin(s)), @(t,s)(cos(t)), [0,2*pi,0,2*pi] )
axis square; title(‘square’)
2.设置视角
例:设置视角
clear
close all
subplot(2,2,1)
ezmesh(@peaks);
view(3);
[a,b]=view;title(mat2str([a,b]))
subplot(2,2,2)
ezmesh(@peaks);
view(2);
[a,b]=view;title(mat2str([a,b]))
subplot(2,2,3)
ezmesh(@peaks);
view([30 45]);
[a,b]=view;title(mat2str([a,b]))
subplot(2,2,4)
ezmesh(@peaks);
view([1 1 sqrt(2)]);
[a,b]=view;title(mat2str([a,b]))
