[Matlab科學繪圖] Matlab三維繪圖總結

主要對matlab三維繪圖函數進行總結並展示一下繪圖結果,重點不在函數解讀,在於函數介紹,函數的具體使用可以查看matlab的help文檔。

函數名 說明
line,plot3,ezplot3 繪製三維曲線
mesh,ezmesh 繪製三維網狀圖
meshc,ezmeshc 繪製帶有等高線的三維網狀圖
meshz 繪製帶有“圍裙”的網狀圖
surf,ezsurf 繪製三維曲面圖
surfc,ezsurfc 繪製帶有等高線的三維曲面圖
surfl 繪製帶有光照的三維曲面圖
surfnorm 計算或者顯示三維表面法向
contour3 繪製三維等高線圖
waterfall 繪製帶有水流效果的三維圖
pcolor 繪製以顏色表示高度的圖形

1. line,plot3,ezplot3繪製三維曲線

t=linspace(0,pi,401); %生成圖形窗口
xf=inline('sin(t*8)*2'); %生成內聯函數
yf=inline('cos(t*8)*3');
s(1)= subplot(131);
%利用函數line繪製三維曲線
line(sin(t*8),cos(t*8),t);
s(2)=subplot(132);
%利用plot3繪製兩條曲線
plot3(sin(t*8)/2,cos(t*8)/2,t,'k',sin(t*16),cos(t*16),t,'r:');
s(3)=subplot(133);
%根據符號表達式繪製三維曲線
ezplot3(xf,yf,inline('t'),[-3,3]);
axis equal;
%設置座標軸視角
view(s(1),[-33,14]);
view(s(2),[-33,14]);
view(s(3),[44,62]);

2.mesh繪製三維網格圖

[X,Y]=meshgrid(-2:0.2:2, -2:0.2:2);%生成座標格網矩陣
Z=X.*exp(-X.^2-Y.^2);
subplot(131);mesh(X,Y,Z);%繪製網狀圖
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(132);mesh(X,Y,Z,rand(size(Z)));%繪製隨機彩色網狀圖
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
subplot(133);mesh(X,Y,Z,2*ones(size(Z)),'EdgeColor','k');%繪製單色網狀圖
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');

3.ezmesh繪製三維網格圖

zfun=inline('sqrt(1-s^2-t^2)');%定義內聯函數
xs=inline('cos(s)*cos(t)');
ys=inline('cos(s)*sin(t)');
zs=inline('sin(s)');
subplot(131);ezmesh(zfun,100);%繪製網狀圖並指明採樣點數
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(132);ezmesh(zfun,[-1,1],20);
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
subplot(133);ezmesh(xs,ys,zs,[-pi,pi],20);
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');

改變ezmesh繪製網狀圖的着色方案

xs=inline('cos(s)*cos(t)');
ys=inline('cos(s)*sin(t)');
zs=inline('sin(s)');
subplot(131);ezmesh(xs,ys,zs,[0,pi],16);
view([-39,56])
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(132);ezmesh(xs,ys,zs,[0,pi],16);
view([-39,56])
S1=get(gca,'Children');
set(S1,'CData',rand(size(get(S1,'CData'))));%通過句柄對網狀圖着色
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
subplot(133);ezmesh(xs,ys,zs,[0,pi],16);
view([-39,56])
S1=get(gca,'Children');
set(S1,'EdgeColor','k');%通過句柄對網狀圖着色
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');

5.meshc繪製帶有等高線的網狀圖

[X,Y,Z]=peaks(30);%生成座標數據
subplot(121),meshc(Z);%生成帶等高線的網狀圖
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(122),m=meshc(X,Y,Z,Z);
set(m(1),'EdgeColor','k');%通過句柄改變屬性
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');

6.ezmeshc繪製帶有等高線的網狀圖

subplot(131),ezmeshc('imag(atan(x+i*y))',[-4,4]);
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(132),ezmeshc('real(log(x+i*y))',[-4,4],30);
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
S2=get(gca,'Children');set(S2(end),'EdgeColor','k');
subplot(133),ezmeshc('real(log(x+i*y))',[-4,4],'circ');
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');

7.meshz繪製帶有“圍裙”的網狀圖

[X,Y,Z]=peaks(30);%生成座標數據
subplot(121),meshz(Z);%生成帶"圍裙"的網狀圖
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(122),m=meshz(X,Y,Z,Z);
set(m(1),'EdgeColor','k');%通過句柄改變屬性
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');

8.surf繪製三維曲面圖

[x,y]=meshgrid(linspace(-3,3,31));%生成格網座標
z=(x-y).*exp(-(x.^2+y.^2)/2);
subplot(121),surf(x,y,z);%繪製曲面圖
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(122),surf(x,y,z,'EdgeColor','flat');
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');

9.ezsurf根據數學表達式繪製三維曲面

funx=inline('sin(s)*t');%定義數學表達式
funy=inline('cos(s)*t');
funz=inline('sinc(t)');
subplot(231),ezsurf(@(x,y) funz(x)*cos(y));%繪製曲面圖
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(232),ezsurf(@(x,y) sin(x*2)*sinc(y),[-pi,pi]);%繪製曲面圖,並設定網格線屬性
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
subplot(233),ezsurf(funx,funy,funz);%繪製曲面圖,並設定網格線屬性
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');
subplot(234),ezsurf(@(s,t) funx(s,t)*sin(t),@(s,t) funy(s,t)*cos(t^2),...
                    @(s,t) funz(t)*sinc(s),[-2,2]);%繪製曲面圖,並設定網格線屬性
xlabel('(d)','Fontsize',14,'Fontname','Times New Roman');
subplot(235),ezsurf(@(s,t) funz(t)*sin(s),30);%繪製曲面圖,並設定網格線屬性
xlabel('(e)','Fontsize',14,'Fontname','Times New Roman');
subplot(236),ezsurf(@(s,t) exp(-[s^2+t^2]),[-2,2],2,'circ');%繪製曲面圖,並設定網格線屬性
xlabel('(f)','Fontsize',14,'Fontname','Times New Roman');

10.surfc繪製帶有等高線的曲面

[x,y]=meshgrid(linspace(-4,4,30));%生成採樣數值點
z=3*(x-1).^2.*exp(-x.^2-(y-1).^2)-8*(x/5-x.^3-y.^5).*exp(-x.^2-y.^2)-...
  1/4*exp(-(x+1).^2-y.^2);
subplot(121),surfc(x,y,z);%繪製三維曲面
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(122),surfc(x,y,z,'EdgeColor','k','FaceColor','None');%設置曲面顏色屬性
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');

ezsurfc繪製帶有等高線的曲面

subplot(121)
ezsurfc(@(x,y)sin(x)*exp(-x.^2-y.^2));
subplot(122)
ezsurfc(@(x,y)sin(2*y)*exp(-x.^2-y^2/2),[-2,2],'circ');
view([-111,42])

11.surfl繪製帶有光照效果的曲面

[x,y,z]=peaks(30);
subplot(121),surfl(x,y,z);
shading interp;%着色淡化處理
colormap(gray);
axis([-3,3,-3,3]);view(3);%設置顏色爲灰度、座標軸範圍和視角
subplot(122)
surfl(x,y,z,[0.8,0.2,0.8],'light');%帶有光照效果
shading interp;
axis([-3,3,-3,3]);view(3);

12.surfnorm計算或者顯示三維表面法向

[X,Y,Z]=peaks(40);
surfnorm(X,Y,Z);xlim([-3,3]);ylim([-3,3]);

13.contour3繪製三維等高線

subplot(121),contour3(peaks(40));%繪製三維等高線
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(122),mesh(peaks(40),'EdgeColor',[0.85,0.85,0.85]);%繪製三維網狀曲面,並設置網格線爲淺灰色
hold on
contour3(peaks(40));
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');

14.waterfall繪製具有流水效果的的曲面,瀑布圖

[x,y,z]=peaks(40);
subplot(131),waterfall(peaks(40));%繪製具有流水效果的曲面
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(132),waterfall(peaks(40),rand(40));%繪製具有流水效果的曲面
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
subplot(133),w=waterfall(x',y',z');%繪製具有流水效果的曲面
set(w,'EdgeColor','k');
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');

15.pcolor繪製以顏色表示高度值的圖形

[x,y,z]=peaks(20);
subplot(131),pcolor(x,y,z);%僞色繪圖
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(132),p=pcolor(x,y,z);
set(p,'EdgeColor','flat');%除去網格線
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
subplot(133),hi=image(z);
set(hi,'CDataMapping','scaled');%設置顏色映像屬性
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');
set(gca,'YDir','normal');

16.曲面切割效果

水平方向切割

[x,y,z]=peaks(800);
subplot(131),surf(x,y,z);shading interp;
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
z(z>4)=2;
z(z<-4)=-4;
subplot(132),surf(x,y,z);
view(-48,52);shading interp;
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
subplot(133),surf(x,y,-z);
view(-48,52);shading interp;
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');

任意切割

[x,y,z]=peaks(200);
z1=z;
z1(x<0&y>0)=nan;%利用NaN切割曲面
z2=z;
z2(sqrt(x.^2+y.^2)<1.2)=nan;
subplot(131),surf(x,y,z);
shading interp;
xlim([-3,3]);ylim([-3,3]);
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(132),surf(x,y,z1);
shading interp;
xlim([-3,3]);ylim([-3,3]);
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
subplot(133),surf(x,y,z2);
shading interp;
xlim([-3,3]);ylim([-3,3]);
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');

17.cylinder生成關於z軸旋轉對稱的螺旋體座標

r=2+cos(linspace(0,pi*2));%生成半徑向量
[x1,y1,z1]=cylinder(r);%生成螺旋體座標
[x2,y2,z2]=cylinder(r,30);
subplot(131),cylinder(r);%生成三維螺旋體
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(132),surf(x1,y1,z1);
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');
subplot(133),mesh(x2,y2,z2);
xlabel('(c)','Fontsize',14,'Fontname','Times New Roman');

18.sphere生成單位球體座標

[x,y,z]=sphere;
subplot(121),sphere;
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(122),surf(x,y,z);
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');

19.slice繪製三維切片圖

[x,y,z]=meshgrid(-2:0.2:2,-2:0.25:2,-2:0.16:2);
v=x.*exp(-x.^2-y.^2-z.^4);
[xi,yi]=meshgrid(-2:0.1:2);
zi=6*exp(-[xi.^2+yi.^2]).*xi;%計算完全切面軸座標
zi(zi>1)=1;zi(zi<-1)=-1;%限制zi取值範圍
subplot(121),slice(x,y,z,v,[-1.2 0.8 2],2,[-2 -0.2]);%繪製切片圖
xlabel('(a)','Fontsize',14,'Fontname','Times New Roman');
subplot(122),slice(x,y,z,v,zi,xi,yi);
xlabel('(b)','Fontsize',14,'Fontname','Times New Roman');

參考文獻

劉正君.《MATLAB科學計算與可視化仿真寶典》

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章