文章目录
1. Advanced 2D plots
1.1 Special Plots
1.1.1 Logarithm Plots 对数图
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取对数
title('Semilogx');
subplot(2,2,3);
semilogy(x,y); % y取对数
title('Semilogy');
subplot(2,2,4);
loglog(x, y); % x, y都取对数
title('Loglog');
% 设置网格
set(gca,'XGrid','on');
plotyy()
% 画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); % AX 表示 axes
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',':');
1.1.2 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');
1.1.3 Bar Charts 条形图
x = [1 2 5 4 8]; y = [x;1:5]; % y = [1 2 5 4 8; 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');
subplot(1,3,3); bar3(y); title('A 3D bargraph');
1.1.4 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');
1.1.5 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]);
1.1.6 Polar Charts 极坐标图
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, 7);
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);
1.1.7 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);
1.1.8 Boxplot and Error Bar
load carsmall
boxplot(MPG, Origin);
x=0:pi/10:pi;
y=sin(x);
e=std(y)*ones(size(x)); % std 求标准差,size(A) 返回一个行向量,其元素包括A的相应维度的长度,ones创建全部为1的数组
errorbar(x,y,e) % e为图中竖线的长度
1.1.9 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');
- EX
t =(1:4)'*pi/2;
x = sin(t);
y = cos(t);
fill(x,y,'y'); axis square off;
text(0,0,'WAIT','Color', 'k', 'FontSize', 70, ...
'FontWeight','bold', 'HorizontalAlignment', 'center');
此练习没有完成全部功能:
1.2 Color Space
- EX
G = [46 38 29 24 13];
S = [29 27 17 26 8];
B = [29 23 19 32 7];
h = [G' S' B'];
b = bar(h,'FaceColor','flat');
for k = 1:size(h,2)
b(1).CData = [1 0.84314 0]; % RGB已修改
b(2).CData = [1 0 0]; % RGB未修改
b(3).CData = [0 0.2 0.2]; % RGB未修改
end
title('Medal count for top 5 countries in 2012 Olympics');
ylabel('Number of medals');
xlabel('Country');
legend('Gold', 'Silver', 'Bronze')