一、Advanced 2D plots
1. Logarithm Plots
x = logspace(-1,1,1000);
% 從-1到1生成等間隔的1000個點
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')
semilogx(x,y);? 對 x 軸取對數
semilogy(x,y);? 對 y 軸取對數
loglog(x,y);??對 x 軸和 y 軸都取對數
2. 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);
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',':');
[AX,H1,H2] = plotyy(x,y1,x,y2);
共用 x 軸,AX返回包含兩個坐標軸句柄的數組[AX(1),AX(2)],分別對應?左側 Y 軸?和?右側 Y 軸
H1、H2:分別返回左側? (y1) 和右側 (y2) 數據曲線的句柄
set(get(AX(1),'Ylabel'),'String','Left Y-axis');
獲得左邊的y軸坐標句柄,將標簽改為Left Y-axis
set(get(AX(2),'Ylabel'),'String','Right Y-axis');
獲得右邊的y軸坐標句柄,將標簽改為Right Y-axis
3.Histogram
y = randn(1,1000);
subplot(2,1,1);
hist(y,10);
title('Bin = 10');
subplot(2,1,2);
hist(y,50);
title('Bin = 50');
hist 將y中所有元素按照bin的個數進行分組,bin等于10,則所有y值,被分為10組,并在圖上用10個等寬度的矩形來表示這10個分組,每個矩陣的高度就表示bin中的元素數量,每個bin中元素的個數是所有y中的元素,依據x軸坐標有序劃入上介于每個bin橫坐標的最大值和最小值間的y元素數量。
4.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');
3D圖形中寬方向表示組別,長方向表示組內元素,高方向表示元素大小
第一個圖條形圖,第二個將兩個組別分開統計做了兩個條形圖
5. Stacked and Horizontal Bar Charts
x = [1 2 5 4 8];
y = [x;1:5];
subplot(1,4,1);
bar(y);
title('y');
subplot(1,4,2);
bar(y,'stacked');
title('y-stacked');
subplot(1,4,3);
barh(y);
title('y-h');
subplot(1,4,4);
barh(y,'stacked');
title('y-h-stacked');
bar后面加h,變成barh表示在生成的條狀圖平行與水平面(horizontal),
' stacked ' 表示在特定方向上的條狀圖堆疊
6. Pie Charts
a = [10 5 20 30];
subplot(2,3,1); pie(a);
subplot(2,3,2); pie(a,[0,0,0,1]);
subplot(2,3,3); pie(a,[1,1,1,1]);
subplot(2,3,4); pie3(a,[0,0,0,1]);
subplot(2,3,5); pie3(a,[1,1,1,1]);
pie(a);? 餅圖,畫扇形圖;pie3()后面加個3代表3D圖形
subplot(2,3,2); pie(a,[0,0,0,1]);??
數組[0,0,0,1]代表扇形分片是否割裂開,0代表不割裂,1代表割裂
7.Polar Char
theta = linspace(0,2*pi)
生成一個從 0 到 2 的等間距角度數組 theta?,默認包含?100 個點。
x = 1:100;theta = x/10;r = log10(x);
subplot(2,3,1);polarplot(theta,r);
theta = linspace(0,2*pi);r = cos(4*theta);
subplot(2,3,2);polarplot(theta,r);
theta = linspace(0,2*pi,6);r = ones(1,length(theta));
subplot(2,3,3);polarplot(theta,r);
theta = linspace(0,2*pi,7);r = ones(1,length(theta));
subplot(2,3,4);polarplot(theta,r);
theta = linspace(0,2*pi);r = 1- sin(theta);
subplot(2,3,5);polarplot(theta,r);
theta = linspace(0,2*pi,6);? ?在0~2范圍內生成 6 個間隔相等的點?
r = ones(1,length(theta));? ? 生成 1 * length(theta)大小的矩陣,且值全為 1
polarplot(theta,r);? theta 極坐標的角度,r 極坐標的半徑
好看b( ̄▽ ̄)d !!
8.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);
函數Stairs()用折線將取的點連起來,函數stem()是做出針狀圖
9.Boxplot and Error Bar
load carsmall.mat
boxplot(MPG,Origin);
① 使用 carsmall.mat??數據集繪制?MPG(每加侖英里數,Miles Per Gallon)
② 按 Origin(汽車原產國)分組對 MPG(每加侖英里數,Miles Per Gallon) 分類的箱線圖
③ 函數boxplot(),boxplot(MPG,Origin)會根據Origin的分組,為每組MPG數據繪制一個箱線圖。箱線圖顯示:中位數、上下四分位數、離群值(圓圈表示)等統計信息。
x = 0: pi/10:pi;
y = sin(x);
e = std(y) * ones(size(x));
errorbar(x,y,e);
使用 std(y) (y的標準差)作為所有數據點的固定誤差值,通過?e = std(y) * ones(size(x));?復制為與x等長的向量。這種處理方式表示所有數據點的誤差相同。誤差條會以垂直線段顯示在每個數據點周圍,線段長度 = e 的值,即 std(y)。
10.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');
t = (1:2:15)' * pi/8;? 生成等差順序的角度序列,,
,
,
......
x = sin(t);? ? %? 將角度轉化為 x 坐標
y = cos(t);? ? % 將角度轉化為 y?坐標
fill(x,y,'r');? ? % 將八邊形內部涂上紅色
axis square off;? ? % 關閉坐標軸的方形設定
text(0,0,'STOP','Color','w','FontSize',80, ...
'FontWeight','bold','HorizontalAlignment','center');? ?
% 設置圖形的樣式 STOP 白色字體放中央,字體大小80,加粗bold,字體放置中央
二、Color Space
顏色有兩種表達方式:
① 0 0 0 表示純黑色,1 1 1 表示純白色,介于 0 0 0 ~ 1 1 1 之間是灰度。
② 0 0 0 表示純黑色,255 255 255 表示純白色,0 255 0 表示純綠色,0 0 255 表示純藍色,
255 0 0 表示純紅色
③ 三個數字分別對應 [ R G B ]? 每個顏色的色度值
G = [46 38 29 24 13]; % 金牌數據(USA, CHN, GBR, RUS, KCR)
S = [29 27 17 26 8]; % 銀牌數據
B = [29 23 19 32 7]; % 銅牌數據% 繪制堆疊條形圖(每組柱子包含G/S/B三個部分)
h = bar(1:5, [G' S' B']); % 設置X軸標簽為國家名稱
set(gca, 'XTickLabel', {'USA','CHN','GBR','RUS','KCR'}); % 自定義顏色(金色、銀色、銅色)
set(h(1), 'FaceColor', [1 0.84 0]); % 金色 (Gold)
set(h(2), 'FaceColor', [0.75 0.75 0.75]); % 銀色 (Silver)
set(h(3), 'FaceColor', [0.8 0.5 0.2]); % 銅色 (Bronze)% 添加標題和軸標簽
title('Medal count for top 5 countries in 2012 Olympics');
xlabel('Country');
ylabel('Number of medals');% 添加圖例
legend('Gold', 'Silver', 'Bronze');
set(h(1), 'FaceColor', [1 0.84 0]); ?
% 金色 (Gold) =? 紅色分量R(1)+ 綠色分量G(0.84)+ 藍色分量(0)
% h(1)第一條柱子顏色
set(h(2), 'FaceColor', [0.75 0.75 0.75]);
% 銀色 (Silver)=? 紅色分量R(0.75)+ 綠色分量G(0.75)+ 藍色分量(0.75)
% h(2)第二條柱子顏色
set(h(3), 'FaceColor', [0.8 0.5 0.2]); ?
?% 銅色 (Bronze)=? 紅色分量R(0.8)+ 綠色分量G(0.5)+ 藍色分量(0.2)
% h(3)第三條柱子顏色
1. Visualizing Data as An Image:imagesc()
① meshgrid(x,y)的功能是:根據給定的向量 x 和 y ,生成二維網格平面上的所有坐標點組合,返回兩個矩陣 x 和 y,分別表示所有點的 x 坐標和 y 坐標。
② surf(x,y,z)函數:根據給定的網格坐標(x,y)和高度值 z?,繪制三維曲面。曲面的顏色默認由?z 值決定,可通過 colormap 修改 。
③ box on 命令:顯示所有三個維度的邊框。
[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');
2. Color Bar and Scheme
colorbar;? 添加顏色條
colormap(cool);colormap(hot);colormap(gray);
3. Built-in Colormaps
每一個colormap都是一個256*3的矩陣
4.Exercise
a = zeros(256,3);
a(:,1) = 0;
a(:,2) = linspace(0.1,0.9,length(a));
a(:,3) = 0;
colormap(a);
x = [1:10;3:12;5:14];
imagesc(x);
colorbar;
① Colormap是 256 * 3 的矩陣,前四行定義 a 矩陣存儲綠色 colormap 的256 * 3 的矩陣
因為是綠色系,所以第一列紅色分量R全為0,且第三列藍色分量B也全為0,
第二列綠色分量G也就是在0~1中生成256個等間距的綠色顏色值,然后把定義的 x 數組涂上顏色
② imagesc(x)是一個用于?可視化矩陣數據?的函數,它會將矩陣?x 的值映射為彩色圖像,并自動縮放顏色范圍以適應數據。
三、3D Plots
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');
x = 0:0.1:3*pi;z1 = sin(x);z2 = sin(2*x);z3 = sin(3*x);
% 設置所有點的 x 坐標和 z 坐標
y1 = zeros(size(x));y3 = ones(size(x));y2 = y3 ./2;
% 設置第一條直線上所有點的y坐標都是0,第二條直線上所有點的y坐標都是0.5,第三條直線上所有點的y坐標都是1
2. More 3D Line Plots
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
grid on;axis square;
螺旋線:橫坐標是 sin(t) 、縱坐標是 cos(t) 、高度是 t
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;
錐形螺旋線:橫坐標是 cos(t) .* (turns - t) ./turns 、縱坐標是 sin(t) .* (turns - t) ./turns?
高度是?t ./ turns;
3.Principles for 3D Surface Plots
meshgrid(x,y)的功能是:根據給定的向量 x 和 y ,生成二維網格平面上的所有坐標點組合,返回兩個矩陣 x 和 y,分別表示所有點的 x 坐標和 y 坐標。
x = -2:1:2;
y = -2:1:2;
[X,Y] = meshgrid(x,y);
4.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(X,Y,Z) 網格曲面圖:僅顯示曲面的?網格線(Wireframe),不填充網格面顏色。
surf(X,Y,Z) 表面曲面圖:顯示曲面的填充面和網格線。
5.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); mesh(X,Y,Z);
axis square;
subplot(1,2,2); contour(X,Y,Z);
axis square;
contour(X,Y,Z)是一個用于繪制?二維等高線圖?的函數,它通過等高線來可視化三維數據?Z?的分布。將三維曲面?Z 投影到二維平面,用閉合曲線(等高線)表示相同?Z 值的區域。
6. Various Contoyr 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,[-0.45:0.05:0.45]);axis square;
subplot(1,3,2); [C,h] = contour(Z);
clabel(C,h); axis square;
subplot(1,3,3); contourf(Z); axis square;
子圖 | 代碼 | 功能 |
---|---|---|
1 | contour(Z,[-0.45:0.05:0.45]) | 手動指定等高線層級(-0.45到0.45,步長0.05) |
2 | [C,h] = contour(Z);clabel (C,h); | 自動分層+添加數值標簽 |
3 | contourf (Z) | 填充顏色的等高線圖(contour后加 f ,意思為 fill ) |
7. Exercise
hold on
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);
clabel(contourf(Z,[-0.45:0.05:0.45])); axis square;
hold off
clabel(contourf(Z,[-0.45:0.05:0.45])); axis square;
函數clabel (C,h) 和 函數contourf (Z)疊一塊使用就得到含有數值的等高線圖
8.meshc() and surfc()
x = -3.5 :0.2:3.5;
y = -3.35: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);
① meshc(X,Y,Z):網格曲面 + 等高線
上方:三維網格曲面,同mesh ;底部:二維等高線投影,同contour?
② sufc(X,Y,Z):表面曲面 + 等高線
上方:三維填充曲面,同surf ;底部:二維等高線投影,同contour?

9.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);
①?sphere(50);?生成單位球面的三維坐標,默認返回50×50的網格,50
?表示球面的網格細分程度,值越大越光滑。
② shading flat ;設置著色模式為?平坦著色,即每個網格面單色
③?light('Position',[1 3 2]);light('Position',[-3 -1 3]);
? 光源三維坐標的位置,決定光照方向和陰影效果,增強立體感
④?material shiny;設置物體表面為?高光材質
⑤?axis vis3d off;vis3d 保持三維比例不變形;'Color',[1 1 1],設置圖形窗口背景為白色
⑥ view(-45,20) :方位角(Azimuth): -45度,從正東方向逆時針旋轉45度;俯仰角(Elevation) :?20 度,向上傾斜20度
10.Ligth:light()
light()函數用于三維圖形中添加光源,通過模擬光照增強立體感和真實感。
L1 = light('Position',[-1,-1,-1]); % 添加光源位置?
set(L1,'Color','g'); % 添加光源色彩
[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,'Positiion',[-1,-1,1]);
set(L1,'Color','g');
① [X, Y, Z] = sphere(64); h = surf(X,Y,Z);
生成一個 64×64 網格的高精度單位球面,并繪制曲面。64是球面的網格細分程度,值越大表面越光滑;h曲面對象的句柄,用于后續屬性修改。
②?axis square vis3d off;
square?保持坐標軸比例一致,vis3d?在旋轉時保持三維視角比例,off 隱藏坐標軸和標簽。
③ reds = zeros(256,3); reds(:,1) = (0:256.-1)/255;
自定義紅色漸變色圖,創建一個 256×3 的零矩陣,僅填充第一列的紅色通道
④?colormap(reds); 應用自定義的紅色漸變色圖
⑤ shading interp; 平滑著色,顏色在網格間漸變,消除網格線
lighting phong;? 使用?Phong 光照,計算高光反射,效果更逼真
⑥?set(h,'AmbientStrength',0.75,'DiffuseStrength',0.5);
'AmbientStrength',0.75:增強環境光和整體亮度
'DiffuseStrength',0.5 :減少漫反射,降低非直射光的影響
11.patch()
patch()函數用于創建?多邊形面片對象,用于繪制自定義 2D/3D 形狀、填充區域或構建復雜幾何體。
參數 | 效果 |
---|---|
' Vertices '? | 邊緣線顏色定義所有的點 |
' Faces ' | 定義所有的面 |
' FaceVertexCData ' | 為8個頂點分配不同顏色,使用 HSV 色圖的前8種顏色 |
' FaceColor ' | 每個面單色,顏色由面的第一個頂點決定 |
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;
view(3)用于設置三維圖形視角的函數,根據當前坐標區的視角切換到默認的三維視圖,默認方位角 -37.5°,默認俯仰角 30°