您的位置:首页 > 编程语言 > MATLAB

Matlab绘图系列之高级绘图

2010-05-25 13:29 375 查看

Matlab绘图系列之高级绘图

原帖地址:

http://blog.163.com/enjoy_world/blog/static/115033832007865616218/

Matlab绘图 2007-09-06 17:06:16 阅读4510 评论10 字号:大中

一、目录
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')


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: