目录
前言
为在B站跟随台湾大学郭彦甫老师学习matlab的笔记,截图来自郭老师PPT,部分内容参考B站弹幕和评论,仅供学习参考。
图形选用
一、进阶 2D 绘图
特殊绘图
对数图
>> x = logspace(-1,1,100);
y = x.^2;
subplot(2,2,1);
plot(x,y);
title('Plot');
subplot(2,2,2);
semilogx(x,y);
title('Semilogx');
subplot(2,2,3);
semilogy(x,y);
title('Semilogy');
subplot(2,2,4);
loglog(x, y);
title('Loglog');
添加网格
set(gca,'XGrid','on');
两个 y 轴 plotyy()
>> 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()
randn() 生成正态分布的随机数
hist() 控制 bin 的个数,显示为矩形,每个矩形高度为在此范围内的数据数目
>> y = randn(1,1000);
subplot(2,1,1);
hist(y,10);
title('Bins = 10');
subplot(2,1,2);
hist(y,50);
title('Bins = 50');
条形图 bar()
bar3() 立体图
>> x = [1 2 5 4 8]; y = [x;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');
堆积条形图和水平条形图
bar(y,'stacked') 堆积
barh(y) 水平
>> x = [1 2 5 4 8];
y = [x;1:5];
subplot(1,2,1);
bar(y,'stacked');
title('Stacked');
subplot(1,2,2);
barh(y);
title('Horizontal');
Exercise
堆叠水平条形图
>> barh(y,'stacked')
饼图 pie()
pie(a,[0,0,0,1]) 控制第四个数据与其他数据分开
pie3() 立体图
>> 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, [0,0,0,1]);
Exercise
分隔饼图中的所有部分
a = [10 5 20 30];
subplot(1,3,1); pie(a, [1,1,1,1]);
subplot(1,3,2); pie(a, [1,1,1,1]);
subplot(1,3,3); pie3(a, [1,1,1,1]);
极图 polar()
polar(theta,r) 绘制极坐标图
>> 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); 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);
Exercise
在极坐标图上绘制六边形
>> theta=linspace(0,2*pi,7);r=ones(1,length(theta));
>> 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);
Exercise
绘制图下函数并使用 stem() 添加在 5 Hz 下采样的点
t=0:0.01:10;
f=sin(t.^2*pi/4);
T=0:0.2:10;
F=sin(T.^2*pi/4);
hold on;
plot(t,f,'b-');
stem(T,F,'r');
hold off;
set(gca,'YTick',-1:0.5:1);
set(gca,'YTickLabel',-1:0.5:1);
箱线图 boxplot() 和误差线 errorbar()
errorbar(x,y,e) e 表示误差棒的长度
x=0:pi/10:pi; y=sin(x);
e=std(y)*ones(size(x));
errorbar(x,y,e)
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');
Exercise
绘制以下图形
t = [0,pi/2,pi,3*pi/2,2*pi]; x = sin(t); y = cos(t);
h=fill(x,y,'y'); axis square off;
set(h,'LineWidth',4);
text(0,0,'WAIT','Color', 'k','FontSize',70, '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');
xlim([-4 4]); xlabel('x'); ylim([-4 4]); ylabel('y');
figure;
imagesc(z);
axis square; xlabel('x'); ylabel('y');
颜色条方案
内置色彩图
colormap(Name) 使用内置色彩方案
三、3D 绘图
plot3()
plot3(x,y,x) 绘制三维的线
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');
mesgrid() 生成网格
绘制三维曲面时需要先生成二维网格
x = -2:1:2;
y = -2:1:2;
[X,Y] = meshgrid(x,y)
mesh() 和 surf()
mesh(X,Y,Z) 和 surf(X,Y,Z) 绘制三维曲面。前者不填充,后者填充
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()
countour(X,Y,Z) 绘制等高线
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;
contour(Z,[]) 可调整等高线密度
clabel(C,h) 标注等高线值
contourf(Z) 填充颜色
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;
Exercise
绘制如下图形
x = -2:0.01:2; y = -2:0.01:2;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-X.^2-Y.^2);
[C,h] = contourf(Z,-.45:.05:.45);
clabel(C,h);
colormap(jet);
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); axis square;
subplot(1,2,2); surfc(X,Y,Z); axis square;
view()
view(alpha,beta) 设置观察视角