matlab学习笔记(五)进阶绘图


参考课程:台大郭彦甫教授MATLAB课程

Advanced 2D plots(2D绘图)

Special Plots

1.Logarithm Plots(对数绘图)

  • logspace(start , end , n):产生n个数,从 1 0 s t a r t 10^{start} 10start~ 1 0 e n d 10^{end} 10end
  • semilogx(x , y):按照 x 轴的对数刻度绘制数据
  • semilogy(x , y):按照 y 轴的对数刻度绘制数据
  • loglog(x , y):按照x、 y 轴的对数刻度绘制数据
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');

在这里插入图片描述

  • plotyy(x , y1 , x , y2):两个y轴同时显示不同的指标刻度值,返回三个句柄
    • [AX , H1 , H2]:返回 AX 中创建的两个坐标区的句柄,以及 H1 和 H2 中每个绘图的图形对象的句柄。AX(1) 是左边的坐标区,AX(2) 是右边的坐标区

如,定义一个这样的函数: y = a e − b x s i n ( c x ) y=ae^{-bx}sin(cx) y=aebxsin(cx)

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

在这里插入图片描述
2.Histogram(直方图):整体

  • hist():绘制直方图,观察数据的整体分布情况
y = randn(1,1000);
subplot(2,1,1);
hist(y,10);
title('Bins = 10');
subplot(2,1,2);
hist(y,50);
title('Bins = 50');

在这里插入图片描述
3.Bar Charts(条形图):具体个别情况

  • bar(y):针对性地绘制某一变量地条形图
  • bar3(y):绘制三维条形图
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');

在这里插入图片描述

  • 条形图堆叠(Stacked)水平条形图(Horizontal)
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');

在这里插入图片描述
【练习1】将水平条形图堆叠:

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

在这里插入图片描述
4.Pie Charts(饼状图)

  • pie(y , [是否要裂开来]):裂开的部分设置为1,其余为0
  • pie3(y , [是否要裂开来]):3D饼状图
a = [10 5 20 30];
subplot(2,2,1); pie(a);
subplot(2,2,2); pie(a , [0,0,0,1]);
subplot(2,2,3); pie(a , [1,1,1,1]);
subplot(2,2,4); pie3(a, [0,0,0,1]);

在这里插入图片描述
5.Polar Chart(极坐标图)

  • polar(角度 θ \theta θ,半径):根据角度半径绘制极坐标图
x = 1:100;
theta = x/10;
r = log10(x);
subplot(2,3,1); polar(theta,r);

theta = linspace(0 , 2 * pi);
r = cos(4 * theta);
subplot(2,3,2); polar(theta,r);

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

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

theta = linspace(0 , 2 * pi , 7);
r = ones(1 , length(theta));
subplot(2,3,5); polar(theta,r);

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

在这里插入图片描述
6.Stairs(阶梯图) 和 Stem(针状图) Charts

  • stairs(y):绘制对应元素y的阶梯图
  • stem(y):绘制对应元素y的离散序列数据的针状图
x = linspace(0 , 4 * pi , 40);
y = sin(x);
subplot(1,2,1); stairs(y);
subplot(1,2,2); stem(y);

在这里插入图片描述

【练习2】
在这里插入图片描述

t=[0:0.2:10];
f=sin((pi * t .^ 2) ./ 4);
h=stem(t,f,'-r');
set(h,'markerEdgecolor','red');
hold on 
t2=[0:0.01:10];
f2=sin((pi * t2 .^ 2) ./ 4);
plot(t2,f2,'b');
yticks([-1:0.5:1]);

在这里插入图片描述

7.Boxplot(箱线图) 和 Error Bar(含误差条的线图)

  • boxplot(数据,分组)
  • errorbar(x , y , 误差)
load carsmall
% 根据样本数据创建每加仑英里数 (MPG) 测量值的箱线图,按车辆的原产国 (Origin) 分组
subplot(1,2,1); boxplot(MPG , Origin); 

x = 0:pi/10:pi;
y = sin(x);
e = std(y) * ones(size(x));
% 创建向量 x 和 y。绘制 y 对 x 的图。在每个数据点处,显示长度相等的垂直误差条
subplot(1,2,2); errorbar(x , y , e);

在这里插入图片描述

Others

  • fill(x , y , color):绘制填充的二维多边形

如,绘制STOP牌:
在这里插入图片描述

t = (1:2:15)' *pi/8; % 等差间隔2Π/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');

在这里插入图片描述
【练习3】
在这里插入图片描述

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

在这里插入图片描述

Color space(色彩空间)

1.[R G B]:0~1之间

  • 0:最小值(黑)
  • 1:最大值(白)
  • 8-bit(0~255)
    在这里插入图片描述
    【练习4】
G = [46 38 29 24 13];
S = [29 27 17 26 8];
B = [29 23 19 32 7];
name = {'USA' 'CHN' 'GER' 'FUS' 'KOR'};
h = bar([G' S' B']);
set(h(1),'FaceColor',[1,0.84314,0])
set(h(2),'FaceColor',[0.5,0.54,0.53])
set(h(3),'FaceColor',[198/255,145/255,69/255])
title('Medal count for top 5 countries in 2012 Olympics');
ylabel('Number of medals'); 
set(gca,'XTickLabel',name);
xlabel('Country');
legend('Gold','Silver','Bronze');

在这里插入图片描述

2.imagesc():

  • imagesc():将数据显示为一个图像,经过标度映射的颜色的图像
  • surf(x , y , z):绘制曲面图
  • colorbar:显示色阶的颜色栏
[x , y] = meshgrid(-3 : .2 : 3,-3 : .2 :3);
z = x .^ 2 + x .* y + y .^ 2;
subplot(1,2,1);
surf(x , y , z); box on;
set(gca , 'FontSize',16);
zlabel('z');
xlim([-4 4]); xlabel('x');
ylim([-4 4]); ylabel('y');
subplot(1,2,2);
imagesc(z); axis square; xlabel('x'); ylabel('y');
colorbar; % 显示色阶的颜色栏

在这里插入图片描述
颜色图:

  • colormap(hot):暖色

在这里插入图片描述

  • colormap(cool):冷色

在这里插入图片描述

  • colormap(gray):灰色

在这里插入图片描述
查看colormap矩阵:

  • a = colormap(prism)

在这里插入图片描述
在这里插入图片描述

3D plot(3D绘图)

在这里插入图片描述
matlab中 2D vs 3D:

x = 0 : 0.1 : 2*pi;
plot(x,sin(x));

在这里插入图片描述
在这里插入图片描述

  • plot3(x , y , z):绘制3D图形

【例1】

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

在这里插入图片描述
【例2】

subplot(1,2,1);
t = 0 : pi/50 : 10*pi;
plot3(sin(t) , cos(t) , t);
grid on; axis square;

subplot(1,2,2);
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;

在这里插入图片描述
3D for Surface Plots

  • meshgrid():生成二维网格,用于绘制3D图形
x = -2 : 1 : 2;
y = -2 : 1 : 2;
[X , Y] = meshgrid(x , y);
Z = X .* Y;

在这里插入图片描述

  • 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); title('Mesh');
subplot(1,2,2); surf(X,Y,Z); title('Surf');

在这里插入图片描述

  • contour(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); title('Mesh'); axis square;
subplot(1,2,2); contour(X,Y,Z); title('Contour'); 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]); title('Contour1'); axis square;
subplot(1,3,2); [C , h] = contour(Z); clabel(C , h); title('Contour2'); axis square;
subplot(1,3,3); contourf(Z); title('Contour3'); axis square;

在这里插入图片描述

  • meshc(x , y , z):绘制网格曲面图下的等高线图
  • surfc(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); meshc(X,Y,Z); title('Meshc'); 
subplot(1,2,2); surfc(X,Y,Z); title('Surfc'); 

在这里插入图片描述
Angel(角度):

  • view():调整视角(照相机视线)

在这里插入图片描述

sphere(50); shading flat;
light('Position' , [1 3 2]);
light('Position' , [-3 -1 3]);
material shiny;
axis vis3d off;
set(gcf , 'Color' , [1 1 1]);
view(-45 , 20);

在这里插入图片描述

Light(光线):

  • light(‘Position’ , 打光位置):调整打光
[X , Y , Z] = sphere(64); 
h = surf(X , Y , Z);
axis square vis3d off;
reds = zeros(256 , 3);
reds(: , 1) = (0:256.-1)/255;
colormap(reds); shading interp; lighting phong;
set(h , 'AmbientStrength' , 0.75 , 'DiffuseStrength' , 0.5);
L1 = light('Position' , [-1 , -1 , -1]);
% set(L1 , 'Position' , [-1 , -1 , 1]);
set(L1 , 'Color' , 'g');

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • patch():绘制填充多边形区域
v = [0 0 0;1 0 0;1 1 0;0 1 0;0.25 0.25 1;...
    0.75 0.25 1;0.75 0.75 1;0.25 0.75 1];
f = [1 2 3 4;5 6 7 8;1 2 6 5;2 3 7 6;3 4 8 7;4 1 5 8];
subplot(1,2,1); patch('Vertices' , v , 'Faces' , f , ...
    'FaceVertexCData' , hsv(6) , 'FaceColor' , 'flat');
view(3); axis square tight; grid on;
subplot(1,2,2); patch('Vertices' , v , 'Faces' , f , ...
    'FaceVertexCData' , hsv(8) , 'FaceColor' , 'interp');
view(3); axis square tight; grid on;

在这里插入图片描述

【例】画个山脉地形图

load cape
X = conv2(ones(9,9)/81 , cumsum(cumsum(randn(100 , 100)) , 2));
surf(X , 'EdgeColor' , 'none' , 'EdgeLighting' , 'Phong' , ...
    'FaceColor' , 'interp');
colormap(map); caxis([-10 , 300]);
grid off; axis off;

在这里插入图片描述

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值