Matlab第五課:進階繪圖
阿新 • • 發佈:2018-11-17
目標:
- 畫2D圖
- 顏色空間
- 3D畫圖
圖的選擇索引:
一、畫2D圖
Special Plots:
相關的函式:
1. Logarithm Plots:關於對數函式的畫圖
- semilogx():對x軸取log
- semilogy():對y軸取log
- loglog():對x軸和y軸都取log
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():畫兩個y軸的影象,返回三個handle(AX:axes,AX(1)和AX(2),H1:y1畫出的先,H2:y2畫出的線)
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',':');
2. Histogram:柱狀圖
hist(data,組數):檢視資料整體分佈
y = randn(1,10000);
subplot(2,1,1);
hist(y,10);
title('Bins = 10');
subplot(2,1,2);
hist(y,50);
title('Bins = 50');
bar():檢視資料的個體分佈
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'); % 3維展示
Stacked and Horizontal Bar:堆積和水平展示
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');
3. pie:餅狀圖
a = [10 5 20 30];
subplot(1,3,1); pie(a);
subplot(1,3,2); pie(a,[0,0,1,1]); % 為1的位置餅圖被分開
subplot(1,3,3); pie3(a,[0,0,0,1]); % 立體圖形展示
4. polar:極座標,利用角度來畫圖
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);
5. staris and stem Charts: 類似於樓梯和柱子
x = linspace(0, 4*pi, 40);
y = sin(x);
subplot(1,2,1);stairs(y); % 階梯
subplot(1,2,2);stem(y); % 圓圈線
6. Boxplot and Error Bar
% boxplot
load carsmall
boxplot(MPG, Origin)
% errorbar
x = 0:pi/10:pi; y = sin(x);
e = std(y)*ones(size(x));
errorbar(x,y,e);
7. 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');
二、Color Space(顏色空間)
- [ R G B]:可以用0-1表示,也可以用0-255表示
- 0 是最小值,黑色
- 1 是最大值,白色
- 8-bit 等值的:
1. VIsualizing Data as An image:imagesc()
[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');
% 二維熱力圖,用顏色表示z軸
imagesc(z);
axis square;
xlabel('x');
ylabel('y')
% 下面的三個命令要分別執行
% 新增顏色深淺標籤
colorbar;
% 熱色系
colormap(hot);
% 冷色系
colormap(cool);
% 灰度
colormap(gray);
相關顏色:
三、3D繪圖
相關3D繪圖函式
1. 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');
2. Principles for 3D Surface Plots:
- 通常需要畫函式:z = f(x, y)
- 需要提供給matlab一個(x, y, z)的要給集合
- 使用 meshgrid(網格)建立關於X和Y的矩陣給定一個範圍
% 生成一個x和y的網格
x = -2:1:2;
y = -2:1:2;
[X,Y] = meshgrid(x,y);
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);
contour():將3D影象投影到2D
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;
meshc() and surfc():
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()
- 變化看圖的角度
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);
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]);
patch():畫多邊形