MATLAB學習筆記五:Advance Plots

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

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章