重点:1.Advanced 2D plots 2. Color Space 3. 3D Plots
x = logspace(-1, 1, 100);
% 表示从 10^(-1)到10^(1) ,并将其99等分,产生100个数
注意:logspace(a,b,n)按 n-1 等分。即:如果要生成10个等分区间,n就要填11。
例:logspace(-1,1,10)是按 9等分,logspace(-1,1,11)是按 10等分,最明显的区别就是n = 11的这个有 10^0=1 的结果。
x = logspace(-1, 1, 100);
y = x .^2;
subplot(2, 2, 1);
plot(x, y);
title('Plot');
subplot(2, 2, 2);
semilogx(x, y);
% 对x轴取log,代替原来的x,就是从-1到0到1
title('Semilogx');
subplot(2, 2, 3);
semilogy(x, y);
% 同理,对y轴取log
title('Semilogy');
subplot(2, 2, 4);
loglog(x, y);
% 对x 和y 都取log
title('Loglog');
对semilog绘图的进一步解释:
“半对数坐标系一个轴是分度均匀的普通坐标轴,另一个轴是分度不均匀的对数坐标轴。该图中的横坐标轴(x轴)是对数坐标。在此轴上,某点与原点的实际距离为该点对应数的对数值,但是在该点标出的值是真数。”
% 加上网格:
set(gca, 'XGrid', 'on');
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);
% plotyy将返回三个handle
set(get(AX(1),'Ylabel'), 'String', 'Left Y-axis')
% 将AX(1)的 Ylabel 设定为 Left Y-axis
set(get(AX(2),'Ylabel'), 'String', 'Right Y-axis')
% 同上
title('Labeling plotyy');
set(H1, 'LineStyle', '--');
set(H2, 'LineStyle',':');
% histogram: 查看整体分布的情况
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];
% x 表示一个矩阵,元素为1 2 5 4 8,
% y表示一个2*5的矩阵,第一行为x,第二行为1 2 3 4 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');
% 对于y,在列上,是different bars,在行上,是different group。
subplot(1, 3, 3); bar3(y); title('A 3D bargraph');
% 在3D维度上看,长宽高分别为:bar、group、values
% Stacked and Horizontal Bar Charts
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');
% 在bar的指令后面加上一个h(表示horizontal),就是将原来的bar水平过来。
第五课课后练习1:
x = [1 2 5 4 8]; y = [x;1:5];
barh(y, 'stacked'); title('Horizontal Stacked')
% Pie Charts
a = [10 5 20 30];
subplot(1, 3, 1); pie(a);
subplot(1, 3, 2); pie(a, [0, 0, 0, 1]);
% 后面的vector就是将原来 a 的Pie Charts分裂开来。
subplot(1, 3, 3); pie3(a, [0, 0, 0, 1]);
第五课课后练习2:
pie(a, [1, 1, 1, 1]);
% Polar Chart: input为(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));
% 五边形的画法:将整个2Π分成5等分,这个的6和最上方的logspace有异曲同工之处,也是-1
subplot(1, 4, 3); polar(theta, r);
theta = linspace(0, 2*pi); r = 1-sin(theta);
subplot(1, 4, 4); polar(theta, r);
第五课课后练习3:
% 要求画出六边形
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);
第五课课后练习4:
t= linspace(0, 10, 1000); y = sin(pi*t.^2./4); plot(t,y);
hold on
t=linspace(0, 10, 50); y=sin(pi*t.^2./4); stem(t,y);
hold off
% Boxplot
load carsmall
boxplot(MPG, Origin);
% 不同国家的汽车,每一加仑的油,可以跑多少迈的统计数字。
% 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);
% 以八边形中心做x、y轴,第一个点与x轴夹角为2*pi/16,往后都是等差数列,加2*pi/8
% 因此各个点的夹角就是[1 3 5…… 15]*pi/8,即t。
% 再对这些夹角t做sin、cos,即得到x坐标和y坐标。
fill(x, y, 'r'); axis square off;
% 这里就已经将封闭图形填色成了红色
text(0, 0, 'STOP', 'Color', 'w', 'FontSize', 80,...
% 这里设置里面字体的颜色,这里是白色
'FontWeight', 'bold', 'HorizontalAlignment', 'center');
第五课课后练习5:
t = [0 1 2 3]'*pi/2; x = sin(t); y = cos(t);
fill(x, y, 'y', 'LineWidth', 5); axis square off;
% 由于结果图形有黑边,所以这里也要设置一下描边,宽度大概为5
text(0, 0, 'WAIT', 'Color', 'k', 'FontSize', 70,...
% 这里的字体大小要改一下,要不然会觉得有点不大协调
'FontWeight', 'bold', 'HorizontalAlignment', 'center');
(FF是十六进制,转成十进制就是255)
第五课课后练习6:
实际在画图时,color是不对的,需要去指定颜色
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']);
set(h(1), 'FaceColor', [1 1 0]); % 设置第一个颜色为金色
set(h(2), 'FaceColor', [.8 .8 .8]); % 设置第二个颜色为银色
set(h(3), 'FaceColor', [1 .8 .6]); % 设置第三个颜色为铜色
% 这里的颜色数值都是通过上面的那个Color Space的图来换算的--先转成十六进制,再/255
title('Medal count for top 5 countries in 2012 Olympics');
ylabel('Number of medals'); xlabel('Country');
legend('Gold', 'Silver', 'Bronze');
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');
% 这张图就是用color代表z轴的深度,越靠近0,越蓝,越往上,越红
imagesc(z); axis square; xlabel('x'); ylabel('y');
画出来发现不像老师画的那样,我的只有黄色就结束了:修改一下默认值
set(0, 'defaultfigureColormap', jet);
有很多时候都不知道一些颜色的数值是多少,这时候要用到colorbar:
imagesc(z); axis square; xlabel('x'); ylabel('y');colorbar;
MATLAB里内带的colormap:
imagesc(z); axis square; xlabel('x'); ylabel('y');
colormap(hot); title ('Hot');
imagesc(z);axis square; xlabel('x'); ylabel('y');
colormap(cool); title('Cool');
imagesc(z); axis square; xlabel('x'); ylabel('y');
colormap(gray); title('Gray');
还可以直接在视图里编辑:
第五课课后练习7:
% 这些题真的,,全靠弹幕大佬
x = [1:10; 3:12; 5:14];
imagesc(x); colorbar;
a1=zeros(1,256);a2=linspace(0,1,256);a3=zeros(1,256);a=[a1' a2' a3'];
colormap(a);
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;
figure
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;
x = -3.5:0.2:3.5; y = -3.5:0.2:3.5;
[X, Y] = meshgrid(x, y); % 用meshgrid函数产生两个不一样的X矩阵和Y矩阵
Z = X.*exp(-X.^2-Y.^2);
subplot(1, 2, 1); mesh(X, Y, Z); % mesh就是指网格,格子上还有空洞
subplot(1, 2, 2); surf(X, Y, Z); % 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;
%% 如果contour比较稀疏,可以通过改变变量来改变图形的疏密:
subplot(1, 3, 1); contour(Z, [-.45: .05: .45]); axis square;
subplot(1, 3, 2); [C, h] = contour(Z);
clabel(C, h); axis square; % clabel不管用可以用contour(Z, 'showtext','on')
subplot(1, 3, 3); contourf(Z); axis square;
% contouf: f 表示fill,即涂上色彩
第五课课后练习8:
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);
[C,h] = contourf(Z,[-.45:.05:.45]);clabel(C,h);
set(gca, 'XTickLabel', -2:1:2); set(gca, 'YTickLabel',-2:0.5:2 );
meshc() 和 surfc() —— 在mesh 和 surf 下面画contour
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(): 可以设定观看物体时的角度
sphere(50); shading flat;
light('Position', [1 3 2]);
light('Position', [-3 -1 3]);
% light 指令用来打光,打光时的角度会有不同的position
material shiny;
axis vis3d off;
set(gcf, 'Color', [1 1 1]);
view(-45, 20);
% 还可以使用图形界面的三维旋转选项,这时左下角会出现观察的球坐标角度表示。
定义light的打光位置和颜色:
第五课课后练习9: