# matlab绘图(四)

x = 0:0.01:pi;

y1 = sin(x);

y2 = cos(x);

% plot：绘制曲线

plot(x,y1)

% plotyy：双y轴

plotyy(x,y1,x,y2)

% bar：条形图

bar(y1(15:20))

% pie：饼状图

pie([2 4 3 5],...

{'North','South','East','West'})

% scatter：散射图

scatter(x(2:15),y1(2:15))

% hist：柱状图

hist(y2)

% feather：矢量图

theta = (-90:10:90)*pi/180;

r = 2*ones(size(theta));

[u,v] = pol2cart(theta,r);

feather(u,v),

axis equal

% compass：矢量图

Z = eig(randn(20,20));

compass(Z)

% contour：等高线

[c,h] = contour(peaks);

clabel(c,h)

% quiver：向量场

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

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

contour(x,y,z);

hold on

quiver(x,y,px,py);

hold off;

% stairs：阶梯图

stairs(y1(1:10))

% stem：杆状图

stem(y2(15:30))

% rose： 玫瑰图

rose(y1,20)

% fill：多边形填充

t = (1/16:1/8:1)'*2*pi;

x = cos(t);

y = sin(t);

fill(x,y,'r')

axis square

% comet：彗星图

t = 0:.01:2*pi;

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

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

comet(x,y);

% polar：极坐标

theta = 0:0.01:2*pi;

rho = sin(2*theta).*cos(2*theta);

figure

polar(theta,rho,'--r')

% semilogx：x对数

x = 0:1000;

y = log(x);

figure;

semilogx(x,y);

% semilogy：y对数

x = 0:0.1:10;

y = exp(x);

figure

semilogy(x,y)

% loglog：双对数

x = logspace(-1,2);

y = exp(x);

figure;

loglog(x,y,'-s');

grid on

% subplot(m,n,p),绘制子图，m代表行数

% n代表列数，p代表绘制第几个子图

x = linspace(-3.8,3.8);

y_cos = cos(x);

y_poly = 1 - x.^2./2 + x.^4./24;

figure

subplot(2,2,1);

plot(x,y_cos);

title('Subplot 1: Cosine');

subplot(2,2,2);

plot(x,y_poly,'g');

title('Subplot 2: Polynomial');

subplot(2,2,[3,4]);

plot(x,y_cos,'b',x,y_poly,'g');

title('Subplot 3 and 4: Both');

% plot3：三维曲线

t = 0:pi/50:10*pi;

st = sin(t);

ct = cos(t);

figure;

plot3(st,ct,t);

% meshgrid：生成网格

[X,Y] = meshgrid(-2:.2:2, -2:.2:2);

% surf

[X,Y] = meshgrid(-2:.2:2, -2:.2:2);

Z = X .* exp(-X.^2 - Y.^2);

surf(X,Y,Z)

% mesh：三维网格图

[X,Y] = meshgrid(-2:.2:2, -2:.2:2);

Z = X .* exp(-X.^2 - Y.^2);

mesh(X,Y,Z)

% sphere：三维球面

[x,y,z] = sphere;

surf(x,y,z)

hold on

surf(x+3,y-2,z)

% centered at (3,-2,0)

surf(x,y+1,z-3)

% centered at (0,1,-3)

% cylinder：柱面

t = 0:pi/10:2*pi;

figure

[X,Y,Z] = cylinder(2+cos(t));

surf(X,Y,Z)

axis square

% peaks：二元高斯分布的概率密度函数

figure

peaks(50);

% waterfall：瀑布图

figure

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

waterfall(X,Y,Z)

% bar3：三维条形图

Y = count(1:10,:);

bar3(Y)

title('Detached Style')

% stem3：三维杆状图

figure

X = linspace(-pi/2,pi/2,40);

Z = cos(X);

stem3(Z)

% pie3：三维饼图

x = [1,3,0.5,2.5,2];

figure

pie3(x)

% fill3：三维多边形填充

X = [0 1 1 2; 1 1 2 2; 0 0 1 1];

Y = [1 1 1 1; 1 0 1 0; 0 0 0 0];

Z = [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];

figure

fill3(X,Y,Z,C)

% comet3：三维彗星轨迹图

t = -10*pi:pi/250:10*pi;

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

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

comet3(x,y,t);

% scatter3：三维散点图

figure

[X,Y,Z] = sphere(16);

x = [0.5*X(:); 0.75*X(:); X(:)];

y = [0.5*Y(:); 0.75*Y(:); Y(:)];

z = [0.5*Z(:); 0.75*Z(:); Z(:)];

scatter3(x,y,z)

% quiver3：向量场

[X,Y] = meshgrid(-2:0.25:2,-1:0.2:1);

Z = X.* exp(-X.^2 - Y.^2);

[U,V,W] = surfnorm(X,Y,Z);

figure

quiver3(X,Y,Z,U,V,W,0.5)

hold on

surf(X,Y,Z)

view(-35,45)

axis([-2 2 -1 1 -.6 .6])

hold off

% contour3：三维等高线

x = -2:0.25:2;

[X,Y] = meshgrid(x);

Z = X.*exp(-X.^2-Y.^2);

contour3(X,Y,Z,30)

subplot(2,2,1),

peaks(50);

subplot(2,2,2),

Y = count(1:10,:);

bar3(Y)

title('Detached Style')

subplot(2,2,3),

[x,y,z] = sphere;

surf(x,y,z)

hold on

surf(x+3,y-2,z) % centered at (3,-2,0)

surf(x,y+1,z-3) % centered at (0,1,-3)

subplot(2,2,4),

[X,Y] = meshgrid(-2:0.25:2,-1:0.2:1);

Z = X.* exp(-X.^2 - Y.^2);

[U,V,W] = surfnorm(X,Y,Z);

quiver3(X,Y,Z,U,V,W,0.5)

hold on

surf(X,Y,Z)

view(-35,45)

axis([-2 2 -1 1 -.6 .6])

hold off

92 篇文章17 人订阅

0 条评论