绘制含有两个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