. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Chapter 3
二維繪圖的進階功能
Hung-Yuan Fan (范洪源)
Department of Mathematics, National Taiwan Normal University, Taiwan
Spring 2020
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 1/76
Outline
L1 二維繪圖額外的繪圖功能 L2 極座標圖
L3 註解並儲存圖形 L4 額外的二維圖形類別
L5 利用 plot 函式對二維陣列繪圖
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Lecture 1
二維繪圖額外的繪圖功能
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 3/76
對數座標的二維圖形
在 x 軸及 y 軸上,線性座標和對數座標將產生四種可能的組合:
1
plot 函式將 x�y 資料都畫在線性軸上。
2
semilogx、semilogy 和 loglog 函式的語法說明如下:
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
對數座標的二維圖形
在 x 軸及 y 軸上,線性座標和對數座標將產生四種可能的組合:
1
plot 函式將 x�y 資料都畫在線性軸上。
2
semilogx、semilogy 和 loglog 函式的語法說明如下:
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 4/76
函式 semilogx 的範例 x = linspace(0,100,600);
semilogx(x,sin(x) ./ (x+1));
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
函式 semilogx 的範例 x = linspace(0,100,600);
semilogx(x,sin(x) ./ (x+1));
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 5/76
函式 semilogy 的範例 x = 1:0.2:12;
y = x.^3 - x + 4;
semilogy(x,y);
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
函式 semilogy 的範例 x = 1:0.2:12;
y = x.^3 - x + 4;
semilogy(x,y);
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 6/76
控制資料點的間距
如何在閉區間 I = [start,end] 上建立等間距的資料點?
1
linspace: 在陣列點之間建立線性的間隔。
linspace(start,end): 在 I 上產生 100 點等間距的陣列。
linspace(start,end,n): 在 I 上產生 n 點等間距的陣列。
2
logspace: 在陣列點之間建立對數的間隔。 函式的形式如下:
y = logspace(se,ee); % 50 點對數尺度上等間距的陣列 y = logspace(se,ee,n);
se 是起始值 start 以 10 為底的次方 (或是指數)。
ee 是終了值 end 以 10 為底的次方 (或是指數)。
n 是所要產生陣列的點數。
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
控制資料點的間距
如何在閉區間 I = [start,end] 上建立等間距的資料點?
1
linspace: 在陣列點之間建立線性的間隔。
linspace(start,end): 在 I 上產生 100 點等間距的陣列。
linspace(start,end,n): 在 I 上產生 n 點等間距的陣列。
2
logspace: 在陣列點之間建立對數的間隔。
函式的形式如下:
y = logspace(se,ee); % 50 點對數尺度上等間距的陣列 y = logspace(se,ee,n);
se 是起始值 start 以 10 為底的次方 (或是指數)。
ee 是終了值 end 以 10 為底的次方 (或是指數)。
n 是所要產生陣列的點數。
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 7/76
範例
>> linspace(1,10,10) ans =
1 2 3 4 5 6 7 8 9 10
>> logspace(0,1,10) % 因為 10 0 = 1 且 10 1 = 10 ans =
1.0000 1.2915 1.6681 2.1544 2.7826 3.5938
4.6416 5.9948 7.7426 10.0000
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例
>> linspace(1,10,10) ans =
1 2 3 4 5 6 7 8 9 10
>> logspace(0,1,10) % 因為 10 0 = 1 且 10 1 = 10 ans =
1.0000 1.2915 1.6681 2.1544 2.7826 3.5938 4.6416 5.9948 7.7426 10.0000
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 8/76
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
雙 y 軸繪圖 (1/2)
函式 plotyy 的範例 x = linspace(0,6,120); figure(1);
plot(x,sqrt(x)+sin(6*x),x,exp(x)); figure(2);
plotyy(x,sqrt(x)+sin(6*x),x,exp(x));
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 10/76
雙 y 軸繪圖 (1/2)
函式 plotyy 的範例 x = linspace(0,6,120);
figure(1);
plot(x,sqrt(x)+sin(6*x),x,exp(x));
figure(2);
plotyy(x,sqrt(x)+sin(6*x),x,exp(x));
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
雙 y 軸繪圖 (2/2)
Figure: 函式 plot(左) 和 plotyy(右) 的繪圖結果
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 11/76
控制 x、y 軸的繪圖範圍
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
函式 axis 的範例
x = linspace(0,10,64);
y = x.*cos(4*x)./12;
figure(1);
plot(x,y,'-ro');
figure(2);
plot(x,y,'-ro');
axis([0,6,-0.6,0.6]);
% 只觀察在 0 ≤ x ≤ 6 和 −0.6 ≤ y ≤ 0.6 範圍內的函數圖形
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 13/76
使用 axis 指令的結果
Figure: 原函數圖形 (左) 和使用 axis(右) 的繪圖結果
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
在相同軸上繪製多個圖形
正常的情況下,每次執行 plot 指令時,都會產生㇐個新的 圖形,同時也會遺失之前顯示在圖上的繪圖資料。
使用 hold on 指令之後,所有其後所產生的圖形,都可以 疊在之前的圖形上。
使用 hold off 指令,可以切換圖形視窗回到預設的狀態。
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 15/76
在相同軸上繪製多個圖形
正常的情況下,每次執行 plot 指令時,都會產生㇐個新的 圖形,同時也會遺失之前顯示在圖上的繪圖資料。
使用 hold on 指令之後,所有其後所產生的圖形,都可以 疊在之前的圖形上。
使用 hold off 指令,可以切換圖形視窗回到預設的狀態。
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
使用 hold on/off 指令的範例 x = linspace(0,2*pi,36);
y1 = sqrt(x).*sin(2*x);
y2 = sqrt(x).*cos(2*x);
plot(x,y1,'-rs');
hold on;
plot(x,y2,'-bo');
hold off;
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 16/76
使用 hold on/off 指令的結果
Figure: 紅色 x-y1 圖形 (左) 和使用 hold on 疊加藍色 x-y2 曲線 (右)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
產生多個圖形視窗
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 18/76
函式 figure 的撰寫格式 .. .
figure(1);
第 1 個圖形視窗的繪圖程式碼 figure(2);
第 2 個圖形視窗的繪圖程式碼 .. .
figure(n);
第 n 個圖形視窗的繪圖程式碼
.. .
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
子圖形
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 20/76
函式 subplot 的範例 (1/4)
>> x = linspace(0,2*pi,50);
>> subplot(2,2,1); plot(x,sin(x));
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
函式 subplot 的範例 (1/4)
>> x = linspace(0,2*pi,50);
>> subplot(2,2,1); plot(x,sin(x));
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 21/76
函式 subplot 的範例 (2/4)
>> subplot(2,2,2); plot(x,cos(x));
>> grid on;
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
函式 subplot 的範例 (2/4)
>> subplot(2,2,2); plot(x,cos(x));
>> grid on;
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 22/76
函式 subplot 的範例 (3/4)
>> subplot(2,2,3); plot(x,sqrt(x));
>> hold on;
>> plot(x,sqrt(x)+sin(2*x));
>>hold off;
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
函式 subplot 的範例 (3/4)
>> subplot(2,2,3); plot(x,sqrt(x));
>> hold on;
>> plot(x,sqrt(x)+sin(2*x));
>>hold off;
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 23/76
函式 subplot 的範例 (4/4)
>> subplot(2,2,4); plot(x,floor(x));
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
函式 subplot 的範例 (4/4)
>> subplot(2,2,4); plot(x,floor(x));
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 24/76
繪製線型的進階控制
使用 plot 函式繪製二維圖形的線型控制:
plot(x,y,'Property',value,…)
Property 說明
LineWidth 設定每條線的寬度,以點為單位。
MarkerEdgeColor 設定標記顏色,或填滿標記的邊緣顏色。
MarkerFaceColor 設定填滿標記的表面顏色。
MarkerSize 設定標記的大小,以點為單位。
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
線型控制的範例 x = 0:pi/15:4*pi;
y = exp(2*sin(x));
plot(x,y,'-ko','LineWidth',3.0,'MarkerSize',6,...
'MarkerEdgeColor','r','MarkerFaceColor','g')
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 26/76
範例的繪圖結果 (承上頁)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
文字字串的進階控制
圖形視窗內的文字字串 (標題、座標軸名等) 的格式,可以使用 流線修飾符號 (stream modifiers) 修改顯示文字的字體格式。
\bf: 粗體文字。
\it: 斜體文字。
\rm: 取消流線修飾符號,恢復預設的文字字體。
\fontname{ fontname }: 設定使用字型的名稱。
\fontsize{ fontsize }: 設定使用字體的大小。
_{xxx}: 下標符號,例如: x_{3} 會顯示為 x 3 。
^{xxx}: 上標符號,例如: x^{3} 會顯示為 x 3 。
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 28/76
常用的希臘文字和數學符號
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例: 圖形視窗出現特殊字體與數學符號 tau = 3;
omega = pi;
t = linspace(0,10);
y = 10 * exp(-t./tau) .* sin(omega*t);
plot(t,y,'b-');
title('Plot of y(t) = 10e^{-t/ \tau}sin(\omega t) ');
xlabel(' \it{t} ');
ylabel(' \it{y(t)} ') ; grid on;
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 30/76
範例的繪圖結果 (承上頁)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Lecture 2 極座標圖
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 32/76
變數 theta 是弧度 (或弳度) 角的陣列,逆時針方向為正。
變數 r 為離原點的距離陣列。
函式 polar 可以輕易地繪製極座標函數
r = f(θ), α ≤ θ ≤ β
的二維圖形。
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例: 心臟線 (Cardioid)
% 繪製函數 r = f(θ) = 1 − cos θ, 0 ≤ θ ≤ 2π, 的圖形。
theta = linspace(0,2*pi, 41);
r = 1 - cos(theta);
polar(theta,r,'r-');
title('Plot of r = 1 - cos \theta');
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 34/76
範例的繪圖結果 (承上頁)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
函式 fplot 的語法
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 36/76
>> fplot('x - cos(x^3)',[-3,3],'ro-.')
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
>> fplot('x - cos(x^3)',[-3,3],'ro-.')
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 37/76
函式 ezplot 的語法
除了繪製㇐般函數圖形之外,ezplot 還可以處理隱函數與參數
方程式的圖形,其使用語法如下:
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
>> ezplot(’sin(1/x)’,[-pi, pi])
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 39/76
>> ezplot(’x^3+4*x^2-3*x+1-y^2’)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
>> ezplot(’x^3+4*x^2-3*x+1-y^2’)
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 40/76
>> ezplot(’3*cos(t)’,’4*sin(t)’,[0,3*pi/2])
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
>> ezplot(’3*cos(t)’,’4*sin(t)’,[0,3*pi/2])
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 41/76
參數曲線的動畫製作
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例: 心臟線的動畫版本
>> t = linspace(0,2*pi,4e4);
>> r = 1 - cos(t);
>> comet(r.*cos(t),r.*sin(t))
% 心臟線將慢慢呈現在您眼前!
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 43/76
範例: 三維參數曲線的動畫版本
>> t = linspace(0,2*pi,4e4);
>> comet3(sin(t/2).*cos(6*t),sin(t/2).*sin(6*t),t)
% 請試試看,見證奇蹟的時刻即將開始!
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例: 三維參數曲線的動畫版本
>> t = linspace(0,2*pi,4e4);
>> comet3(sin(t/2).*cos(6*t),sin(t/2).*sin(6*t),t)
% 請試試看,見證奇蹟的時刻即將開始!
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 44/76
Lecture 3
註解並儲存圖形
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
圖形內的說明文字
Recall: 設定圖形標題和座標軸的解說文字,可使用下列函式:
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 46/76
加入圖形的註解文字
為了提高繪製圖形的可讀性,MATLAB 提供下列函式:
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
使用 legend 和 text 的範例 x = linspace(0,2*pi,36);
y1 = x.*cos(x);
y2 = x.*sin(x);
plot(x, y1, '-rs', x, y2, '-bo');
legend('x*cos(x)', 'x*sin(x)', 'Location', 'NW');
text(2, 2.5, 'x*sin(x)');
text(5.5, 3, 'x*cos(x)');
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 48/76
範例的繪圖結果 (承上頁)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Lecture 4
額外的二維圖形類別
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 50/76
MATLAB 除了提供㇐些基本的二維繪圖函式以外,還另外提供 了下列常見的統計圖形:
⻑桿圖 (stem plots)
⻑條圖、直條圖 (bar plots) 圓形圖 (pie plots)
直方圖 (histogram)
階梯圖 (stair plots)
羅盤圖 (compass plots)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
統計繪圖函式的語法
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 52/76
⻑桿圖的範例
x = [1 2 3 4 5 6];
y = [2 6 8 7 8 5];
stem(x,y);
title(' \bf Example of a Stem Plot');
xlabel(' \bf \it x');
ylabel(' \bf \it y');
axis([0 7 0 10]);
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例的圖結果 (承上頁)
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 54/76
直條圖的範例
x = [1 2 3 4 5 6];
y = [2 6 8 7 8 5];
bar(x,y);
title(' \bf Example of a Bar Plot');
xlabel(' \bf \it x');
ylabel(' \bf \it y');
axis([0 7 0 10]);
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例的繪圖結果 (承上頁)
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 56/76
橫條圖的範例
x = [1 2 3 4 5 6];
y = [2 6 8 7 8 5];
barh(x,y);
title(' \bf Example of a Horizontal Bar Plot');
xlabel(' \bf \it y');
ylabel(' \bf \it x');
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例的繪圖結果 (承上頁)
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 58/76
圓形圖的範例
data = [10 37 5 6 6];
explode = [0 1 0 0 0];
pie(data,explode);
title(' \bf Example of a Pie Plot');
legend('One','Two','Three','Four','Five');
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例的繪圖結果 (承上頁)
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 60/76
三維圓形圖的範例 data = [10 37 5 6 6];
explode = [0 1 0 0 0];
pie3(data,explode);
title(' \bf Example of a 3D Pie Plot');
legend('One','Two','Three','Four','Five');
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
範例的繪圖結果 (承上頁)
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 62/76
直方圖的繪圖語法
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
>> data = [0 3 3 4 5 3 7 4 2 8 2 8 10];
>> v = hist(data)
v =
1 2 3 2 1 0 1 2 0 1
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 64/76
>> data = [0 3 3 4 5 3 7 4 2 8 2 8 10];
>> v = hist(data) v =
1 2 3 2 1 0 1 2 0 1
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
>> data2 = randn(10000,1);
>> hist(data2,20)
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 65/76
>> data2 = randn(10000,1);
>> hist(data2,20)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
階梯圖的範例
x = [1 2 3 4 5 6];
y = [2 6 8 7 8 5];
stairs(x,y);
title(' \bf Example of a Stair Plot');
xlabel(' \bf \it x');
ylabel(' \bf \it y');
axis([0 7 0 10]);
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 66/76
範例的繪圖結果 (承上頁)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
羅盤圖的範例
x = [3 4 2 -3 -3];
y = [3 2 -2 -3 2.5];
compass(x,y);
title(' \bf Example of a Compass Plot');
與極座標圖形相似,但不是取 (r, θ) 為繪圖資料點。
仍是以直角坐標 (x, y) 作為繪圖資料點,並將原點與資料點 之間的連線段以箭號表示。
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 68/76
範例的繪圖結果 (承上頁)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Lecture 5
利用 plot 函式對二維陣列繪圖
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 70/76
㇐般而言,繪圖資料點都是儲存在㇐維陣列或向量。
如果我們擁有的是二維陣列資料而不是向量資料,那會怎 樣?
MATLAB 會把二維陣列的 每㇐行視為㇐條線,而且資料集
合內有多少行,就會畫出多少條線。
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
輸入引數為二維陣列的範例 x = 0:0.1:10;
y = zeros(length(x),4);
y(:,1) = sin(x);
y(:,2) = cos(x);
y(:,3) = sin(x).^2;
y(:,4) = cos(x).^2;
plot(x,y);
title(' \bf Example of a 2D Array Plot');
xlabel(' \bf \it x');
ylabel(' \bf \it y');
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 72/76
範例的繪圖結果 (承上頁)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
輸入引數為二維陣列的直條圖 x = 1:5;
y = zeros(5,3);
y(1,:) = [1 2 3];
y(2,:) = [2 3 4];
y(3,:) = [3 4 5];
y(4,:) = [4 5 4];
y(5,:) = [5 4 3];
bar(x,y);
title(' \bf Example of a 2D Bar Plot');
xlabel(' \bf \it x');
ylabel(' \bf \it y');
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 74/76
範例的繪圖結果 (承上頁)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Thank you for your attention!
Hung-Yuan Fan (范洪源), Dep. of Math., NTNU, Taiwan Chap. 3, Computer Programming 76/76