MATLAB 进阶绘图


前言

b站课程《MATLAB教程_台大郭彦甫(14课)》学习记录


在这里插入图片描述

一、Special Plots

在这里插入图片描述

二、Logarithm Plots 对数图像

logspace():

  1. logspace(X1,X2),即在10X1-10X2范围内等距产生50个行向量;
  2. logspace(X1,X2,N),即在10X1-10X2范围内等距产生N个行向量。
    X1,X2支持float( double/single)型。

subplot(n,m,p):
生成n*m个区域,这个图画在第p个区域

semilogx(Y) :该函数对x轴的刻度求常用对数以(10为底),而y轴为线性刻度
semilogx(X1,Y1,X2,Y2,…):同一坐标系中画出多条不同颜色的线条
semilogx(X1,Y1,LineSpec1,X2,Y2,LineSpec2,…):比上一条参数类型多了LineSpecn,它可以指定使用线型、标记符号和颜色
semilogy:与semilogx类似,顾名思义以y轴采用对数坐标
loglog:x轴和y轴均采用对数坐标,调用命令格式与semilogx相同
————————————————
【版权声明:本内容为CSDN博主「执念斩长河」的原创文章内容,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37149062/article/details/107302811】

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()

在一张图中有两个纵坐标,自变量相同,因变量不同
[AX,H1,H1] = plotyy(X,Y1,X,Y2)
H1指左侧因变量对应的图线,H2指右侧因变量对应的直线

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

在这里插入图片描述

四、Histogram 直方图,柱状图

看整体的情况
randn()产生乱数,并且呈normal distribution(正态分布)
hist(y,10) 10指的是有10个柱形

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 Charts 柱形图表,条线图

看个体的情况

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

bar3()画出三维的图像,分别为bar,group,value

在这里插入图片描述

六、Stacked and Horizontal Bar Charts 堆叠和水平条形图

x = [1 2 5 4 8]; 
y = [x;1:5];
subplot(1,2,1); 
bar(y,'stacked');  堆叠起来加'stacked'
title('Stacked');
subplot(1,2,2); 
barh(y); 
title('Horizontal');

barh 为水平放置
在这里插入图片描述

Exercise: stack the horizontal bar chart

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,'stacked'); 
title('Horizontal');

在这里插入图片描述

七、Pie Charts 饼状图

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

pie3也为立体的
在这里插入图片描述

Exercise: separate all the pieces in the pie chart

a = [10 5 20 30];
subplot(1,3,1); pie(a);
subplot(1,3,2); pie(a, [1,1,1,1]);
subplot(1,3,3); pie3(a, [0,0,0,1]);

在这里插入图片描述
中间一幅图即为完全分开的

八、Polar Chart 极坐标图

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

ones(a,b)表示的产生一个ab的矩阵,里面的元素全是1
theta = linspace(0, 2
pi, 6)
将图进行五等份,6取了六个点,是因为第一个点为0,最后一个点为6,两者已经重复了
length()相当于矩阵/向量的维数
在这里插入图片描述

Exercise: plot a hexagon(六边形) on a polar chart

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

在这里插入图片描述

九、Stairs and Stem Charts

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

在这里插入图片描述

Exercise

在这里插入图片描述

x = linspace(0, 10, 1000);
y1 = sin((pi.*x.^2)/4);
t = linspace(0, 10, 50);
y2 = sin((pi.*t.^2)/4);
hold on;
plot(x,y1,'b');
stem(t,y2,'r');
hold off;

在这里插入图片描述

十、Boxplot and Error Bar 箱线图和误差条

在这里插入图片描述

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

在这里插入图片描述

十一、fill()

Stop sign

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

Plot a wait sign

%方法一
t =0:pi/2:2*pi; x = cos(t); y = sin(t);
fill(x,y,'y','Linewidth',5); axis square off;
text(0,0,'WAIT','Color', 'k', 'FontSize', 70, 'FontWeight','bold', 'HorizontalAlignment', 'center');
%方法二
t = (0: 1: 4)'*pi/2; x = cos(t); y = sin(t);
fill(x,y,'y','Linewidth',5); axis square off;
text(0,0,'WAIT','Color', 'k', 'FontSize', 70, 'FontWeight','bold', 'HorizontalAlignment', 'center');

在这里插入图片描述

十二、Color Space

[R G B]
0 is minimum
1 is maximum
8-bit equivalence:
在这里插入图片描述
在这里插入图片描述

Exercise

补全以下代码,使其运行结果为所示图形:

G = [46 38 29 24 13]; S = [29 27 17 26 8];
B = [29 23 19 32 7]; h = bar(1:5, [G' S' B']);
title('Medal count for top 5 countries in 2012 Olympics');
ylabel('Number of medals'); xlabel('Country');
legend('Gold', 'Silver', 'Bronze')

在这里插入图片描述

G = [46 38 29 24 13]; S = [29 27 17 26 8];
B = [29 23 19 32 7]; h = bar(1:5, [G' S' B']);
title('Medal count for top 5 countries in 2012 Olympics');
ylabel('Number of medals'); xlabel('Country');
legend('Gold', 'Silver', 'Bronze')
h(1).FaceColor = hex2dec(['FB'; 'D8'; '60'])/255;
h(2).FaceColor = hex2dec(['C0'; 'C0'; 'C0'])/255;
h(3).FaceColor = hex2dec(['B8'; '73'; '33'])/255;
set(gca,'XTickLabel',{'USA','CHN','GBR','RUS','KOR'});
【h(1).FaceColor = '#FBD860';
h(2).FaceColor = '#C0C0C0';
h(3).FaceColor = '#B87333';】

十三、Visualizing Data as An Image: imagesc()

Display values of a matrix as an “image”

[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');
colormap(jet);  使图变为彩色

在这里插入图片描述

[x, y] = meshgrid(-3:.2:3,-3:.2:3); 
z = x.^2 + x.*y + y.^2; surf( x, y, z); box on; 
imagesc(z); axis square; xlabel('x'); ylabel('y'); 
colormap(jet);


【转载】MATLAB三维绘图基础meshgrid函数的用法解析
MATLAB中meshgrid函数是用来生成网格的,函数用法是:
[X,Y] = meshgrid(x,y);这种是最常用的一种用法。x和y分别是两个向量。使用示例:
​​​​在这里插入图片描述

结果:

在这里插入图片描述
A中的每个点对应的是x轴的坐标点,B中的每个点对应的是y轴的坐标点,讲的有点抽象,下面画图来说明一下。
绘制出来的坐标是:
在这里插入图片描述

坐标所对应的点是:
在这里插入图片描述
  其实A表示将从第一行开始到最后一行的x轴的坐标值为A矩阵的行,所以按照上图所示A矩阵就是:
在这里插入图片描述
  B表示将从第一列开始到最后一列的y轴的坐标值为B矩阵的列,按照上图所示B矩阵就是:
在这里插入图片描述
所以可以知道meshgrid函数的本质是确定x,y坐标轴上每个位置的值。这个在绘制三维图的时候非常重要,因为三维图其实就是根据x,y平面的每个位置上对应着一个特定的z,然后将它绘制出来,就是所谓的三维图。
根据以上原理简单绘制一个三维图,示例:

%% 学习画三维图形
% meshgrid 函数是用来生成一个网格
clear; clc; close all;
[x,y] = meshgrid(1:0.5:10,1:20);  % 生成网格
z = sin(x) + cos(y);
surf(x,y,z);  % 画图函数

效果显示:
在这里插入图片描述
由图示可以知道,如果x,y的坐标越精确,则绘制出来的三维图越细腻。
示例:
在这里插入图片描述
效果显示:
在这里插入图片描述
surf(X,Y,Z) 创建一个三维曲面图,它是一个具有实色边和实色面的三维曲面。该函数将矩阵 Z 中的值绘制为由 X 和 Y 定义的 x-y 平面中的网格上方的高度。曲面的颜色根据 Z 指定的高度而变化。
此外,surf(X,Y,Z,C) 还指定曲面的颜色。
box on 为显示框轮廓

十四、Color Bar(显示色阶的颜色栏) and Scheme(按色调布置)

​colorbar;
在这里插入图片描述
colormap(hot)
在这里插入图片描述
colormap(cool)
在这里插入图片描述
colormap(gray)

在这里插入图片描述

十五、Built-in Colormaps

Use built-in color maps:

colormap([Name])

A color map is a matrix of 256X3

a = colormap(prism)

Use a customized color map:

a = ones(256,3);
colormap(a);

在这里插入图片描述

Exercise

Create a custom green color map such that the output of the script below looks like:
在这里插入图片描述

x = [1:10; 3:12; 5:14];
imagesc(x);
colorbar;
map = zeros(256,3);
map(:,2) = (0:255)/255;
colormap(map);

根据【R,G,B】进行渐变型改变

十六、3D Plots

在这里插入图片描述

十七、2D vs. 3D

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

MATLAB里面基本都是3D的,点击工具栏中的三维旋转,即可转动图形,显示三维的样子
在这里插入图片描述

十八、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');

在这里插入图片描述

十九、More 3D Line Plots

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;

在这里插入图片描述

二十、Principles for 3D Surface Plots

Usually for plotting functions: 𝑧 = 𝑓(𝑥, 𝑦)
Need to provide MATLAB a set of (𝑥, 𝑦, 𝑧) points
Use meshgrid to create matrices X and Y for a given range
使用网格创建给定范围的矩阵X和Y

>> x = -2:1:2;
y = -2:1:2;
[X,Y] = meshgrid(x,y)
X =
    -2    -1     0     1     2
    -2    -1     0     1     2
    -2    -1     0     1     2
    -2    -1     0     1     2
    -2    -1     0     1     2
Y =
    -2    -2    -2    -2    -2
    -1    -1    -1    -1    -1
     0     0     0     0     0
     1     1     1     1     1
     2     2     2     2     2

二十一、Surface Plots: mesh() and 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);

mesh是空洞格子
surf是涂色格子
在这里插入图片描述

二十二、contour()

Projection of equal heights of 3D plot onto a 2D plane
在二维平面上绘制三维图形

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

在这里插入图片描述

二十三、Various Contour Plots

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

Combine the contour techniques to generate a figure as shown below
在这里插入图片描述

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(X,Y,Z,-.45:.05:.45); 
clabel(C,h);
colormap(jet);
axis square;

二十五、meshc() and surfc()

Combination of surface/mesh and contours

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

在这里插入图片描述

二十六、View Angle: view()

Vary the view angle
view(-45,20);
in the script below
在这里插入图片描述
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); //更换角度所示图不同
colormap(jet);
在这里插入图片描述

二十七、Light: light()

[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()

A graphical object containing polygons

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;

在这里插入图片描述

Exercise

MATLAB plots can be very professional!

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;

在这里插入图片描述


总结

继续加油吧。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值