MATLAB初接触的进阶绘图相关知识

绘制二维面

一、折线图:

1、plot()、loglog()、semilogx() 与 semilogy()

loglog():x轴和y轴都取对数坐标
semilogx():x轴取对数坐标,y轴取线性坐标
semilogy():x轴取线性坐标,y轴取对数坐标

x = logspace(-1,1,100); //产生变数的函数,数值是从10的-1次方到10的1次方,共100个
y = x.^2;               //y=x的二次方,也就是10的-2次方到10的2次方,共100个

subplot(2,2,1);
plot(x,y);
title('Plot');          //线性方式画

subplot(2,2,2);
semilogx(x,y);
title('Semilogx');      //x轴取对数坐标,y轴取线性坐标

subplot(2,2,3);
semilogy(x,y);
title('Semilogy');      //x轴取线性坐标,y轴取对数坐标

subplot(2,2,4);
loglog(x, y);
title('Loglog');        //x轴和y轴都取对数坐标

在这里插入图片描述
小提示 loglog()画图时最好加网格,这样才能更为清楚的看出差距
在这里插入图片描述

2、plotyy():带有两个y坐标轴的线性坐标系

x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);

[AX,H1,H2] = plotyy(x,y1,x,y2);
set(get(AX(1),'Ylabel'),'String','Left Y-axis')
set(get(AX(2),'Ylabel'),'String','Right Y-axis')
title('Labeling plotyy');
set(H1,'LineStyle','--'); set(H2,'LineStyle',':');

在这里插入图片描述

二、直方图:hist()

x = randn(1,1000);  //产生乱数的函数,有1000个

subplot(2,1,1);
hist(x,10);
title('Bins = 10');

subplot(2,1,2);
hist(x,50);
title('Bins = 50');

在这里插入图片描述

三、柱状图:bar()

bar()绘制二维
bar3()绘制三维

x = [1 2 5 4 8]; 
y = [x;1:5];      //第一个是x:1 2 5 4 8,第二个是1~5

subplot(1,3,1); 
bar(x); 
title('A bargraph of vector x');

subplot(1,3,2); 
bar(y); 
title('A bargraph of vector y');

subplot(1,3,3); 
bar3(y); 
title('A 3D bargraph');

在这里插入图片描述
barh()绘制纵向排列的柱状图

x = [1 2 5 4 8];
y = [x;1:5];
barh(y);
title('Horizontal');

在这里插入图片描述
向bar()传入’stack’参数,让柱状图以堆栈的形式画出

x = [1 2 5 4 8];
y = [x;1:5];
bar(y,'stacked');
title('Stacked');

在这里插入图片描述

四、饼状图:pie()

a = [10 5 20 30];

subplot(1,3,1); 
pie(a);

subplot(1,3,2); 
pie(a, [0,0,0,1]);    //30所占的饼状图部分分开

subplot(1,3,3); 
pie3(a, [0,0,0,1]);   //三维

在这里插入图片描述

五、极坐标图:ploar()

x = 1:100;

//螺旋线
theta = x/10; 
r = log10(x);

subplot(1,4,1); 
polar(theta,r);

//花瓣
theta = linspace(0, 2*pi); 
r = cos(4*theta);

subplot(1,4,2); 
polar(theta, r);

//五边形
theta = linspace(0, 2*pi, 6);   //o~2pi分为五等分六个顶点
r = ones(1,length(theta));

subplot(1,4,3); 
polar(theta,r);

//心形线
theta = linspace(0, 2*pi); 
r = 1-sin(theta);

subplot(1,4,4); 
polar(theta , r);

在这里插入图片描述

六、阶梯图stairs()和针状图stem()

绘制离散数字序列

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

在这里插入图片描述

七、实图:fill()

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

在这里插入图片描述

八、设置颜色相关

绘制一幅图

[x, y] = meshgrid(-3:.2:3,-3:.2:3); z = x.^2 + x.*y + y.^2; 
surf(x,y,z)

在这里插入图片描述
美化一下

box on;                    //加外框

set(gca,'FontSize',16);    //放大

zlabel('z');
xlabel('x');
ylabel('y');               //给坐标轴加备注

xlim([-4 4]);
ylim([-4 4])               //改变坐标域

在这里插入图片描述

imagesc(z)

在这里插入图片描述

colorbar

在这里插入图片描述

colormap(hot)   //括号内可换其他色系如cool、gray等等

在这里插入图片描述

绘制三维面

一、plot3()

输入应为三个向量.

x=0:0.1:3*pi; 
z1=sin(x); 
z2=sin(2.*x); 
z3=sin(3.*x);
y1=zeros(size(x)); 
y2=y3./2;
y3=ones(size(x));
 
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; 
axis square;

在这里插入图片描述
圆锥:

turns = 40*pi;
t = linspace(0,turns,4000);
x = cos(t).*(turns-t)./turns;
y = sin(t).*(turns-t)./turns;
z = t./turns;
plot3(x,y,z); 
grid on;

在这里插入图片描述

二、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);     //不填充网格
subplot(1,2,2); 
surf(X,Y,Z);     //填充网格

在这里插入图片描述

三、contour()和contourf()

绘制三维图形的等高线

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); 
axis square;
subplot(1,2,2);
contour(X,Y,Z); 
axis square;

在这里插入图片描述

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,3,1); 
contour(Z,[-.45:.05:.45]); 
axis square;

subplot(1,3,2); 
[C,h] = contour(Z); 
clabel(C,h); 
axis square;

subplot(1,3,3); 
contourf(Z); 
axis square;

在这里插入图片描述

四、meshc()和surfc()

在绘制三维图形时绘制其等高线

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); 
meshc(X,Y,Z);

subplot(1,2,2); 
surfc(X,Y,Z);

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值