Matlab繪圖系列之高級繪圖


 

一、目錄

1.彗星圖

   二維彗星圖

   三維彗星圖

2.幀動畫

3.程序動畫

4.色圖變換

5.Voronoi圖和三角剖分

   Voronoi圖

   三角剖分

6.四面體

7.綵帶圖

   綵帶圖

   三維流綵帶圖

8.僞彩圖

9.切片圖

   切片圖

   切片輪廓線圖

10.輪廓圖

   顯示輪廓線

   顯示圍裙

   瀑布效果

   帶光照模式的陰影圖

11.函數繪圖

   輪廓線、網格圖、曲面圖、輪廓網格圖

   輪廓曲面圖、二維曲線、極座標曲線圖、自定義函數

12.三維圖形控制

   視點

   燈光效果

   色彩控制

二、圖形示例

1.彗星圖

二維彗星圖

t=0:.01:2*pi;

x=cos(2*t).*(cos(t).^2);

y=sin(2*t).*(sin(t).^2);

comet(x,y);

title('二維彗星軌跡圖')

hold on

plot(x,y)

 

三維彗星圖

a=12;

b=9;

T0=2*pi;%T0是軌道的週期

T=5*T0;

dt=pi/100;

t=[0:dt:T]';

f=sqrt(a^2-b^2);%地球與另一焦點的距離

th=12.5*pi/180;%未經軌道與x-y平面的傾角

E=exp(-t/20);%軌道收縮率

x=E.*(a*cos(t)-f);

y=E.*(b*cos(th)*sin(t));

z=E.*(b*sin(th)*sin(t));

plot3(x,y,z,'g')%畫全程軌線

hold on,sphere(20);%畫地球

axis off

title('衛星返回地球示例')

x1=-18*T0;

x2=6*T0;

y1=-12*T0;

y2=12*T0;

z1=-6*T0;

z2=6*T0;

axis([x1 x2 y1 y2 z1 z2])

% axis([-15 10 -15 10 -10 10])

axis equal

comet3(x,y,z,0.02);%畫運動軌線

hold off

 

2.幀動畫

Z=peaks;

surf(Z)%繪製網格表面圖

axis tight

set(gca,'nextplot','replacechildren');%設定axis覆蓋重畫模式

title('幀動畫播放示例')

for j=1:20

    surf(sin(2*pi*j/20)*Z,Z)%重新繪製網格表面圖,這裏後面一個Z當成了顏色矩陣

    F(j)=getframe;%創建幀

end

movie(F,20)%播放動畫20次

 

3.程序動畫

t=0:pi/50:10*pi

i=1;

h=plot3(sin(t(i)),cos(t(i)),t(i),'*','erasemode','none');%設定擦除模式

grid on

axis([-2 2 -2 2 -1 10*pi])

title('程序動畫示例')

for i=2:length(t)

    set(h,'xdata',sin(t(i)),'ydata',cos(t(i)),'zdata',t(i));

    drawnow

    pause(0.01)

end

 

4.色圖變換

load spine

image(X)

colormap cool

spinmap(10)

 

5.Voronoi圖和三角剖分

Voronoi圖

rand('state',5)

x=rand(1,10);

y=rand(1,10);

subplot(131)

voronoi(x,y);%繪製voronoi圖形

axis equal

axis([-0.2 1.6 -0.5 2.5])

subplot(132)

[vx,vy]=voronoi(x,y);

plot(x,y,'r+',vx,vy,'b-');%應用返回值繪製

axis equal

axis([-0.2 1.6 -0.5 2.5])

subplot(133)

rand('state',5);

x=rand(10,2);

[v,c]=voronoin(x);%返回值v參數維voronoi頂點矩陣,返回值c參數爲voronoi元胞數組

for i=1:length(c)

    if all(c{i}~=1)

        patch(v(c{i},1),v(c{i},2),i);%應用色圖i

    end

end

axis equal

axis([-0.2 1.6 -0.5 2.5])

box on

       三角剖分 

[x,y]=meshgrid(1:15,1:15);

tri=delaunay(x,y);

z=peaks(15);

trimesh(tri,x,y,z)

      

 

6.四面體

d=[-1 1];

[x,y,z]=meshgrid(d,d,d);%定義一個立方體

x=[x(:);0];

y=[y(:);0];

z=[z(:);0];%[x,y,z]分別爲加上中心的立方體頂點

X=[x(:) y(:) z(:)];

Tes=delaunayn(X);%返回m×n的數組值

tetramesh(Tes,X);%繪製四面體圖

camorbit(20,0);%旋轉攝像目標位置

 

7.綵帶圖

綵帶圖

[x,y]=meshgrid(-3:.5:3,-3:.1:3);

z=peaks(x,y);

ribbon(y,z)

    

 

三維流綵帶圖

load wind%打開保存的數據

lims=[100.64 116.67 17.25 28.75 -0.02 6.86];%定義座標軸範圍

[x,y,z,u,v,w]=subvolume(x,y,z,u,v,w,lims);%lims來定義數據子集

[sx sy sz]=meshgrid(110,20:5:30,1:5);%定義網格點

verts=stream3(x,y,z,u,v,w,sx,sy,sz,.5);%計算綵帶頂點

cav=curl(x,y,z,u,v,w);%計算捲曲角速度

wind_speed=sqrt(u.^2+v.^2+w.^2);%計算流速

h=streamribbon(verts,x,y,z,cav,wind_speed,2);%繪製流綵帶圖

view(3)

 

8.僞彩圖

n=6%定義輪數

r=(0:n)'/n;%定義輪的半徑

theta=pi*(-n:n)/n;%定義輪的扇區角

X=r*cos(theta);

Y=r*sin(theta);%定義網格頂點

C=r*cos(2*theta);%定義色圖

pcolor(X,Y,C)%繪製僞彩圖axis equal tight

 

 

9.切片圖

切片圖

[x,y,z] = meshgrid(-2:.2:2,-2:.25:2,-2:.16:2);

v = x.*exp(-x.^2-y.^2-z.^2);

xslice = [-1.2,.8,2]; yslice = 2; zslice = [-2,0];

slice(x,y,z,v,xslice,yslice,zslice)

colormap hsv

 

切片輪廓線圖

[x y z v]=flow;%打開水流數據

h=contourslice(x,y,z,v,[1:9],[],[0],linspace(-8,2,10));%切片輪廓線

view([-12 30])

 

10.輪廓圖

顯示輪廓線

[x,y,z]=peaks;

subplot(1,2,1)

meshc(x,y,z);%同時畫出網格圖與輪廓線

title('meshc 網格圖與輪廓線')

axis([-inf inf -inf inf -inf inf]);

subplot(1,2,2)

surfc(x,y,z);%同時畫出曲面圖與輪廓線

title('surfc 曲面圖與輪廓線')

axis([-inf inf -inf inf -inf inf]);

 

顯示圍裙

[x y z]=peaks;

meshz(x,y,z);

 

 

瀑布效果

[X,Y,Z]=peaks(30);

waterfall(X,Y,Z)

    

 

帶光照模式的陰影圖

[x,y]=meshgrid(-3:1/8:3);

z=peaks(x,y);

surfl(x,y,z);

shading interp%着色處理

colormap(gray);%灰度處理

axis([-3 3 -3 3 -8 8])

 

11.函數繪圖

輪廓線、網格圖、曲面圖、輪廓網格圖

%圖1繪製輪廓線、網格圖、曲面圖、輪廓網格圖

subplot(221)

f=['3*(1-x)^2*exp(-(x^2)-(y+1)^2)-10*(x/5-x^3-y^5)*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2)'];%定義雙變量x、y的函數式

ezcontour(f,[-3,3],49)%x、y爲[-3 3],網格爲49×49

subplot(222)

ezmesh('sqrt(x^2+y^2)');

subplot(223)

ezsurf('real(atan(x+i*y))')%經過濾波,如果相同數據surf繪圖沒有濾波

subplot(224)

ezmeshc('y/(1+x^2+y^2)',[-5,5,-2*pi,2*pi])%x、y的數值範圍分別爲[-5 5]、[-2*pi 2*pi]

      

輪廓曲面圖、二維曲線、極座標曲線圖、自定義函數 

%圖2繪製輪廓曲面圖、二維曲線、極座標曲線圖、自定義函數

figure(2)

subplot(221)

ezsurfc('sin(u)*sin(v)')

subplot(222)

ezplot('x^2-y^4');

subplot(223)

ezpolar('1+cos(t)')

subplot(224)

fplot('myfun',[-20 20])

 

function Y=myfun(x)

Y(:,1)=200*sin(x(:))./x(:);

Y(:,2)=x(:).^2;

     

三維曲線圖 

%繪製三維曲線圖

figure(3)

ezplot3('sin(t)','cos(t)','t',[0,6*pi])

   

 

12.三維圖形控制

視點

View

圖形旋轉

subplot(121)

surf(peaks);

title('旋轉前圖形');

subplot(122)

h=surf(peaks);

rotate(h,[1 0 1],180)

title('旋轉後圖形');

 

燈光效果

%燈光效果(1)camlight(2)light(3)lightangle(4)lighting(5)material

sphere;

camlight

 

色彩控制

%色彩控制(1)缺省設置colordef、whitebg(2)色圖colormap(3)濃淡處理shading

load flujet

image(X)

colormap(jet) 

 

subplot(131)

sphere(16)

axis square

shading flat

title('Flat Shading')

subplot(132)

sphere(16)

axis square

shading faceted

title('Faceted Shading')

subplot(133)

sphere(16)

axis square

shading interp

title('Interpolated Shading')

 

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