目錄
1、繪製三維曲線圖
plot3爲三維繪圖的基本函數,格式爲:
plot3(x,y,z,LineSpec,...),x,y,z爲向量,LineSpec爲定義曲線線型。
x=[1 2 3 4 5];
y=[1 2 3 4 5];
z=[1 2 3 4 5];
plot3(x,y,z)
grid
2、繪製三維網格圖
mesh爲三維網格圖繪製函數,格式爲: mesh(x,y,z)。另外還有meshc:用來繪製等值線網格圖,meshz:用來繪製包含0平面的網格圖。
[x,y,z]=peaks(50);
subplot(131);mesh(x,y,z)
subplot(132);meshc(x,y,z)
subplot(133);meshz(x,y,z)
另外命令hidden off使網格圖不透明顯示,如下:
[x,y,z]=sphere(15);%球
subplot(121);
mesh(x,y,z)
title('不透明球');
hidden off
subplot(122);
mesh(x,y,z)
title('透明球');
3、 繪製三維曲面圖
surf繪製着色的三維曲面圖,格式爲:surf(x,y,z)。另外有surfc:繪製了底層等高線圖,surfl:考慮了有光照效果的表面圖。
函數shading flat:平面陰影,shading interp:插值陰影。
[x,y,z]=peaks(18);
subplot(131);surf(x,y,z),title('surf');
subplot(132);surfc(x,y,z),title('surfc');
subplot(133);surfl(x,y,z),title('surfl');
4、特殊圖形繪製
(1)繪製圓柱型
格式爲:[x,y,z]=cylinder,[x,y,z]=cylinder(r),[x,y,z]=cylinder(r,n),對於這三個函數來說高度都爲1,地面半徑分別爲:1,r,r,距離相同的點有20,20,n。
cylinder(2)
(2) 繪製球型
格式爲:sphere(n)。
sphere(30)
(3) 繪製三維離散數據
格式爲:stem3(x,y,z,c,'filled'),其中c代表線條顏色,filled填充點。
x=rand(3);
stem3(x,'bo','filled')
(4)等高線和帶狀圖
二維等高線圖:contour(),三維等高線圖:contour3(),帶狀圖:ribbon(x,y)。
[x,y,z]=peaks(15);
m=peaks(20);
subplot(131);contour(x,y,z,20)
subplot(132);contour3(x,y,z,20)
subplot(133);ribbon(m)
5、立體可視化
(1)視覺化作圖
x=0:0.2:5;
y=1:0.3:6;
z=3:0.25:8;
[x1,y1,z1]=meshgrid(x,y,z);
v=x1+y1+z1;
slice(x1,y1,z1,v,[4.2],[3],[5.25])
xlabel('x-axis');ylabel('y-axis');zlabel('z-axis');
(2)四維表現圖
x=0:0.2:5;
y=1:0.3:6;
z=3:0.25:8;
[x1,y1,z1]=meshgrid(x,y,z);
v=x1+y1+z1;
xslice=[3,4,5];yslice=[4];zslice=[3,5];
slice(x1,y1,z1,v,xslice,yslice,zslice)
colormap hsv %colormap是MATLAB裏面用來設定和獲取當前色圖的函數。
xlabel('x-axis');ylabel('y-axis');zlabel('z-axis');
6、視角控制
用於視角控制的函數爲:view,viewmtx,rotate3d。
[x,y,z]=peaks(20);
subplot(221);surf(x,y,z),title('默認視角');
subplot(222);surf(x,y,z),view(0,90),title('view(0,90)');
subplot(223);surf(x,y,z),view(-60,60),title('view(-60,60)');
subplot(224);surf(x,y,z),view(30,78),title('view(30,78)');