文章目录
- 前言
- 一、Special Plots
- 二、Logarithm Plots 对数图像
- 三、plotyy()
- 四、Histogram 直方图,柱状图
- 五、Bar Charts 柱形图表,条线图
- 六、Stacked and Horizontal Bar Charts 堆叠和水平条形图
- Exercise: stack the horizontal bar chart
- 七、Pie Charts 饼状图
- Exercise: separate all the pieces in the pie chart
- 八、Polar Chart 极坐标图
- Exercise: plot a hexagon(六边形) on a polar chart
- 九、Stairs and Stem Charts
- Exercise
- 十、Boxplot and Error Bar 箱线图和误差条
- 十一、fill()
- Exercise
- 十二、Color Space
- Exercise
- 十三、Visualizing Data as An Image: imagesc()
- 十四、Color Bar(显示色阶的颜色栏) and Scheme(按色调布置)
- 十五、Built-in Colormaps
- Exercise
- 十六、3D Plots
- 十七、2D vs. 3D
- 十八、plot3()
- 十九、More 3D Line Plots
- 二十、Principles for 3D Surface Plots
- 二十一、Surface Plots: mesh() and surf()
- 二十二、contour()
- 二十三、Various Contour Plots
- Exercise
- 二十五、meshc() and surfc()
- 二十六、View Angle: view()
- 二十七、Light: light()
- 二十八、patch()
- Exercise
- 总结
前言
b站课程《MATLAB教程_台大郭彦甫(14课)》学习记录
一、Special Plots
二、Logarithm Plots 对数图像
logspace():
- logspace(X1,X2),即在10X1-10X2范围内等距产生50个行向量;
- 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, 2pi, 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;
总结
继续加油吧。