matlab绘图(四)

感谢大家关注matlab爱好者公众号,今天给大家介绍matlab常用的二维和三维绘图命令。在聊天栏中回复“012”、“二维”或“三维” 即可快速获取本文章。通过访问下面网站即可观看高清视频:https://v.qq.com/x/page/t0630lrrg3t.html

今天介绍绘图的最后一部分内容,绘图部分的介绍先告一段落,后面讲到相关部分再作补充。

视频内容

二维

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);

[px,py] = gradient(z,.2,.15);

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:三维条形图

load count.dat

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),

load count.dat

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

温馨提示

如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。

原文发布于微信公众号 - matlab爱好者(matlabaihaozhe)

原文发表时间:2018-04-15

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券