1、几何图形技术Matlab高级绘图资料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=si
2、n(2*t).*(sin(t).2);comet(x,y);title(二维彗星轨迹图)hold onplot(x,y)三维彗星图a=12;b=9;T0=2*pi;%T0是轨道的周期T=5*T0;dt=pi/100;t=0:dt:T;f=sqrt(a2-b2);%地球与另一焦点的距离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);%画地球axi
3、s offtitle(卫星返回地球示例)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 equalcomet3(x,y,z,0.02);%画运动轨线hold off2帧动画Z=peaks;surf(Z)%绘制网格表面图axis tightset(gca,nextplot,replacechildren);%设定axis覆盖重画模式title(帧动画播放示例)for j=1:20 surf(sin(2*pi*j/20)*Z,Z
4、)%重新绘制网格表面图,这里后面一个Z当成了颜色矩阵 F(j)=getframe;%创建帧endmovie(F,20)%播放动画20次3程序动画t=0:pi/50:10*pii=1;h=plot3(sin(t(i),cos(t(i),t(i),*,erasemode,none);%设定擦除模式grid onaxis(-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)end4色图变换load spineimage(
5、X)colormap coolspinmap(10)5Voronoi图和三角剖分Voronoi图rand(state,5)x=rand(1,10);y=rand(1,10);subplot(131)voronoi(x,y);%绘制voronoi图形axis equalaxis(-0.2 1.6 -0.5 2.5)subplot(132)vx,vy=voronoi(x,y);plot(x,y,r+,vx,vy,b-);%应用返回值绘制axis equalaxis(-0.2 1.6 -0.5 2.5)subplot(133)rand(state,5);x=rand(10,2);v,c=vorono
6、in(x);%返回值v参数维voronoi顶点矩阵,返回值c参数为voronoi元胞数组for i=1:length(c) if all(ci=1) patch(v(ci,1),v(ci,2),i);%应用色图i endendaxis equalaxis(-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
7、,z分别为加上中心的立方体顶点X=x(:) y(:) z(:);Tes=delaunayn(X);%返回mn的数组值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
8、=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*co
9、s(2*theta);%定义色图pcolor(X,Y,C)%绘制伪彩图axis equal tight9切片图切片图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);%切片轮廓线v
10、iew(-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=meshg
11、rid(-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(-(x2)-(y+1)2)-10*(x/5-x3-y5)*exp(-x2-y2)-1/3*exp(-(x+1)2-y2);%定义双变量x、y的函数式ezcontour(f,-3,3,49)%x、y为-3 3,网格为4949subplot(222)ezmesh
12、(sqrt(x2+y2);subplot(223)ezsurf(real(atan(x+i*y)%经过滤波,如果相同数据surf绘图没有滤波subplot(224)ezmeshc(y/(1+x2+y2),-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(x2-y4);subplot(223)ezpolar(1+cos(t)subplot(
13、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
14、(5)materialsphere;camlight色彩控制%色彩控制(1)缺省设置colordef、whitebg(2)色图colormap(3)浓淡处理shadingload flujetimage(X)colormap(jet)subplot(131)sphere(16)axis squareshading flattitle(Flat Shading)subplot(132)sphere(16)axis squareshading facetedtitle(Faceted Shading)subplot(133)sphere(16)axis squareshading interptitle(Interpolated Shading)