a1,二维绘图plot的实现
subplot(n,m,x),建立一个m*n的绘图区域,然后分别在其x=1,2,3,4...区域绘制图像。
function [] = plot1()
x=0:0.35:7;
y=2*exp(-0.5*x);
subplot(2,2,1);bar(x,y,'g');
title('bar(x,y,''g'')');axis([0, 7, 0 ,2]);
subplot(2,2,2);fill(x,y,'r');
title('fill(x,y,''r'')');axis([0, 7, 0 ,2]);
subplot(2,2,3);stairs(x,y,'b');
title('stairs(x,y,''b'')');axis([0, 7, 0 ,2]);
subplot(2,2,4);stem(x,y,'k');
title('stem(x,y,''k'')');axis([0, 7, 0 ,2]);
绘制的图形如下所示:
bar(x,y,'g'):柱形图
fill(x,y,'r'):填充
stairs(x,y,'b'):梯形图
stem(x,y):茎状图
2,三维绘图简单应用:
function [] = plotThree()
% x=sin(theta),y=cos(theta),z=cos(4*theta)
theta = 0:0.01*pi:2*pi;
x = sin(theta);
y = cos(theta);
z = cos(4*theta);
subplot(2,2,1);
%打开坐标显示
%axis on
plot3(x,y,z,'LineWidth',2);
hold on;
% x=sin(theta),y=cos(theta),z=cos(4*theta)
theta = 0:0.02*pi:2*pi;
x = sin(theta);
y = cos(theta);
z = cos(4*theta);
subplot(2,2,2);
plot3(x,y,z,'rd','MarkerSize',10,'LineWidth',2);
% 准备数据
%meshgrid网络点,二维数据
[X,Y] = meshgrid(-1:0.1:1);
Z = sin(X.^2.*pi) + cos(Y.*pi);
% 设置无限远平行光源光照效果
subplot(2,2,3);
%创建一个三维曲面图
surf(X,Y,Z);
%在当前坐标区中创建一个光源。光源仅影响补片和曲面图对象。
light('Style','infinit','Position',[0 -0.6 1]);
title('无限远平行光')
% 设置本地光源辐射源光照效果
subplot(2,2,4);
surf(X,Y,Z);
light('Style','local','Position',[0 -0.6 1]);
title('本地辐射光')
显示如下:
3,三维图绘画:
function [] = plotshere()
[x,y,z] = sphere(25);
subplot(3,4,1);surf(x,y,z);
axis equal;shading interp;
hold on;
title('lighting none')
subplot(3,4,2);surf(x,y,z);
axis equal;
light('position',[0,0.5 1]);
shading interp;lighting flat;
hold on;
title('lighting flat');
subplot(3,4,3);surf(x,y,z);
axis equal;
light('position',[0,0.5 1]);
shading interp;lighting gouraud;
hold on;
title('lighting gouraud');
subplot(3,4,4);surf(x,y,z);
axis equal;
light('position',[0,0.5 1]);
shading interp;lighting phong;
hold on;
title('lighting phong');
subplot(3,4,5);
set(gcf,'color','w');
sphere(25);
axis vis3d
h = light;
for az = -50:10:50
lightangle(h,az,30)
pause(.2)
end
t = 0:pi/10:2*pi;
[X1,Y1,Z1] = cylinder(2 + cos(t));
subplot(3,4,6);surf(X1,Y1,Z1)
axis square;title('三维柱面图');
subplot(3,4,7);sphere
axis equal;title('三维球体');
x1 = [1 3 0.5 2.5 2];
explode = [0 1 0 0 0];
subplot(3,4,8);pie3(x1,explode)
title('三维饼图');axis equal;
X2 = [0 1 1 2;1 1 2 2;0 0 1 1];
Y2 = [1 1 1 1;1 0 1 0;0 0 0 0];
Z2 = [1 1 1 1;1 0 1 0;0 0 0 0];
C = [0.5000 1.0000 1.0000 0.5000;
1.0000 0.5000 0.5000 0.1667;
0.3330 0.3330 0.5000 0.5000];
subplot(3,4,9);fill3(X2,Y2,Z2,C);
colormap hsv
title('三维填充图');axis equal;
[x2,y2] = meshgrid(-3:.5:3,-3:.1:3);
z2 = peaks(x2,y2);
subplot(3,4,10);ribbon(y2,z2)
colormap hsv
title('三维彩带图');axis equal;
[X3,Y3] = meshgrid(-2:0.25:2,-1:0.2:1);
Z3 = X3 .* exp(-X3.^2 - Y3.^2);
[U,V,W] = surfnorm(X3,Y3,Z3);
subplot(3,4,11);quiver3(X3,Y3,Z3,U,V,W,0.5);
hold on
surf(X3,Y3,Z3);
colormap hsv
view(-35,45);
title('三维向量场图');axis equal;
set(gcf,'Color','w');
[x y z v] = flow;
h = contourslice(x,y,z,v,[1:9],[],[0],linspace(-8,2,10));
subplot(3,4,12);
axis([0,10,-3,3,-3,3]);daspect([1,1,1])
camva(24);
camproj perspective;
campos([-3,-15,5])
set(gcf,'Color',[.5,.5,.5],'Renderer','zbuffer')
%set(gca,'Color','black','XColor','white','YColor','white','Zcolor','white')
box on