Matlab第四課:基礎繪圖
目標:
- 基礎繪圖
- 繪圖的物件
一、基礎繪圖
plot()函式:
- plot(x, y):根據座標(x,y)畫出圖形
- plot(y):只給出y,這樣畫圖的時候,x軸就是x=[1...n], n = length(y)
- 但我們想要在畫布上使用兩個plot()時。第二個圖形會覆蓋第一個
hold on/off:
- 作用:在一個畫布上,畫多個圖形
>> hold on; >> plot(cos(0:pi/20:2*pi)); >> plot(sin(0:pi/20:2*pi)); >> hold off;
畫圖風格引數:
legend():新增圖例
x = 0:0.5:4*pi;
y = sin(x);
h = cos(x);
w = 1./(1+exp(-x));
g = (1/(2*pi*2)^0.5).*exp((-1.*(x-2*pi).^2)./(2*2^2));
plot(x,y,'bd-',x,h,'gp:',x,w,'ro-',x,g,'c^-');
% 新增圖例
legend('sin(x)','cos(x)','Sigmoid','Gauss function');
title() and xlabel() and ylabel():新增影象標題與x軸和y軸標籤
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = exp(-x);
plot(x, y1, '--*', x, y2, ':o');
% 新增x軸y軸標籤
xlabel('t = 0 to 2\pi');
ylabel('values of sin(t) and e^{-x}');
% 新增標題
title('Function Plots of sin(t) and e^{-x}');
legend('sin(t)', 'e^{-x}');
text() and annotation():在圖形上顯示文字和畫箭頭
x = linspace(0,3); y = x.^2.*sin(x); plot(x,y); line([2,2], [0,2^2*sin(2)]); str = '$$ \int_{0}^{2} x^2\sin(x) dx $$'; % 顯示出積分公式 text(0.25, 2.5, str, 'Interpreter','latex'); % 顯示箭頭 annotation('arrow', 'X', [0.32,0.5], 'Y', [0.6,0.4]);
二、繪圖物件
matlab畫圖包含的結構:
-
Figure object:畫布
-
Axes object:座標軸
-
Line object:圖形
-
Text object:文字
-
Surface object
-
...
-
Figure Adjustment:
幾個特性:
-
字型
-
字型大小
-
線的寬度
-
座標限制
-
tick positon(刻度位置)
-
tick labe
Modifying Properties of An Object(修改繪圖物件)
策咯:
-
找到物件的handle,每一個object都有一個handle
-
讀取或改變物件的特性
1.identifying the handle of an object:獲取物件的handle
-
Upon creation
-
h = plot(x,y); 直接獲取畫線的handle
-
-
Utility functions:
2.Fetching or Modigying Properties:取出或者改變物件
-
get object properties(獲取物件的特性):get()
-
setting axes limits(設定座標軸的限制):set()
-
set(gca, 'XLim', [0, 2*pi]);
-
-
setting font and tick of axes(改變座標軸的字型大小,刻度)
-
set(gca, 'FontSize', 25);
-
set(gca, 'XTick', 0:pi/2:2*pi);
-
set(gca, 'XTickLabel', 0:90:360);
-
-
Line Specification(線的特性)
-
line style and width(線的風格和寬度)
-
set(h,'LineStyle','-.','LineWidth',7.0,'Color','g');
-
-
-
Marker specification(線上標記的特性)
-
face and edge colors of the markder
-
Multiple Figures(多個畫布):
-
呼叫figure建立一個figure視窗。
-
當使用gcf 獲取handel時要小心,存在多個figures。
x = -10:0.1:10;
y1 = x.^2 - 8;
y2 = exp(x);
% 呼叫figure,畫出y1
figure, plot(x,y1);
% 呼叫figure,畫出y2
figure, plot(x,y2);
-
Figure Position and Size
-
figure('Position',[left,bottom,width,height]);
-
Several Plots in one Figure:
-
several small plots "in a figure"
-
subplot(m,n,1); % 在一個figure上畫,m行n列個圖,1代表第幾個圖
-
t = 0:0.1:2*pi;
x = 3*cos(t);
y = sin(t);
subplot(2,2,1);plot(x,y); axis normal
subplot(2,2,2);plot(x,y); axis square
subplot(2,2,3);plot(x,y); axis equal % 正常圖,x和y的刻度一樣
subplot(2,2,4);plot(x,y); axis equal tight
操作figure的幾個函式:注意當多個圖形時,只操作最後一個
Saving Figures into Files :
如果想到達到高解析度,則使用print函式儲存檔案