matlab绘图(续)

本文详细介绍了如何在MATLAB中使用各种函数绘制含有两个y轴的图像,统计图(包括直方图和不同bins),柱状图(包括普通、堆叠和横向),饼状图,极坐标图,箱型图,三维绘图,等高线图以及多边形和球体的绘制。展示了如何调整颜色、轴范围和光源等,以实现丰富的数据可视化效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

绘制含有两个y轴的图像:
 

x=0:0.1:20;
y1=200*exp(-0.05.*x).*sin(x);
y2=0.8*exp(-0.5.*x).*sin(x);
title("plot")


yyaxis left%绘制左边轴的图像
plot(x,y1)
ylabel("left side")

yyaxis right%绘制右边轴的图像
plot(x,y2)
ylabel("right side")

绘制结果:

统计图:

y=randn(1,1000);
subplot(2,1,1)
histogram(y);
title("bins=10")
subplot(2,1,2)
histogram(y,100)
title('bins=50')

上述代码生成一千个正态分布的数:

柱状图:

x=[1 2 5 4 8];
y=[x;1:5];
subplot(1,3,1);bar(x);
subplot(1,3,2);bar(y);
subplot(1,3,3);bar3(y);%三维柱状图

绘制其他风格的柱状图:

x=[1 2 5 4 8];
y=[x;1:5];
subplot(1,2,1);bar(y,'stacked');%堆叠绘制
subplot(1,2,2);barh(y);%横向绘制

G=[46 38 29 24 13]; S=[29 27 17 26 8]; B=[29 23 19 32 7];
bar({'usa','chn','uk','rus','kcr'},[G;S;B]);
xlabel('contry'); ylabel('medal counts');
legend('Gold','Silver','Bronze');

饼状图

a=[10 5 20 30];
subplot(1,3,1);pie(a);
subplot(1,3,2);pie(a,[0,0,0,1]);%指定分割区域
subplot(1,3,3);pie3(a);%三维饼状图

分割所有区域:

a=[10 5 20 30];
pie(a,[1,1,1,1]);

Polar Chart(极坐标图)

x=1:100; theta=x/10; r=log10(x);
subplot(1,4,1); polarplot(theta,r);

theta=linspace(0,2*pi); r=cos(4*theta);
subplot(1,4,2); polarplot(theta,r);

theta=linspace(0,2*pi,6); r=ones(1,length(theta));
subplot(1,4,3); polarplot(theta,r);

theta=linspace(0,2*pi); r=1-sin(theta);
subplot(1,4,4); polarplot(theta,r);

阶梯和杆状图:

x=linspace(0.4*pi,40); y=sin(x);
subplot(1,2,1); stairs(y);
subplot(1,2,2); stem(y);

箱型图和含误差条线图:

rng default  % For reproducibility
x = randn(100,25);

subplot(3,1,1)
boxplot(x)

subplot(3,1,2)
boxplot(x,'PlotStyle','compact')

x=0:pi/10:pi; y=sin(x);
e=std(y)*ones(size(x));
subplot(3,1,3)
errorbar(x,y,e);

填充:

t=(1:2:15)'*pi/8; x=sin(t); y=cos(t);
fill(x,y,'r'); axis square off %关闭坐标轴
text(0,0,'STOP','Color','w','FontSize',80,'FontWeight','bold', ...
    'HorizontalAlignment','center')

t=(0:2:6)'*pi/4; x=sin(t); y=cos(t);
fill(x,y,'y','LineWidth',5); axis square off 
text(0,0,'WAIT','Color','k','FontSize',60,'FontWeight','bold', ...
    'HorizontalAlignment','center')

三维绘图:

[x,y]=meshgrid(-3:.3:3,-3:.2:3);
z=x.^2+x.*y+y.^2; surf(x,y,z); box on
set(gca,'FontSize',16); zlabel('z');
xlim([-4 4]); xlabel('x'); ylim([-4 4]); ylabel('y')

显示深浅:

[x,y]=meshgrid(-3:.1:3,-3:.1:3);
z=x.^2+x.*y+y.^2; 
set(gca,'FontSize',16); 
imagesc(z); xlabel('x'); ylabel('y');
colorbar;%显示z轴深度

添加指令colormap(cool)或colormap(hot)将图像改为冷色系或暖色系

此外还有colormap(gray)

plot3

x=0:0.1:3*pi; z1=sin(x); z2=sin(2*x); z3=sin(3*x);
y1=zeros(size(x)); y3=ones(size(x)); y2=y3./2;
plot3(x,y1,z1,'r',x,y2,z2,'b',x,y3,z3,'g'); grid on; 
xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');

t=0:pi/50:10*pi;
plot3(sin(t),cos(t),t); grid on; 
xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');

使用meshgrid函数创建x和y轴的矩阵

注意观察mesh函数和surf函数绘制图像的差异

x=-3.5:0.2:3.5; y=-3.5:0.2:3.5;
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
subplot(1,2,1); mesh(X,Y,Z);title('mesh')
subplot(1,2,2); surf(X,Y,Z);title('surface')
colormap winter

绘制球:

sphere(50); 
shading flat%渲染模式
axis square

添加代码light('Position',[-1,-1,-1]);设置光源位置

L1=light('Position',[-1,-1,-1]);
set(L1,'Color','w')

通过以上代码设置打光颜色

绘制多边形:

由于绘制多边形较为复杂,因此请自行前往官网查阅

等高线图:

x=-3.5:0.2:3.5; y=-3.5:0.2:3.5;
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
contour(X,Y,Z);%使用contour函数绘制
colormap winter

meshc和surfc(在图像底部多绘制一个等高线)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值