plot(x,y) %描繪(x,y)點(diǎn),點(diǎn)的個(gè)數(shù)點(diǎn)的密度是由x確定的咒精,因?yàn)閥可以是一個(gè)函數(shù),是將x代進(jìn)去算出的值
plot(y) %描繪(x,y)點(diǎn),x=[1,2,3...n]鳍征,n=length(y)
例:plot( cos(-2*pi : pi/20 : 2*pi) )
cos.jpg
?
?
Snip20180213_1.png
?
?
-
使用 hold on 指令涯呻,會將所有圖像繪制到一個(gè)figure中凉驻。
例:
hold on
plot( cos(0 : pi/20 : 2*pi), 'o--g' )
plot( sin(0 : pi/20 : 2*pi), 'x:r' )
hold off
holdon.jpg
?
?
-
一條指令畫多個(gè)圖像
-
圖例
例:
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^-'); %一條指令畫四個(gè)曲線
legend('sin(x)', 'cos(x)', 'Sigmoid', 'Gauss function'); %圖例
legend.jpg
?
?
-
title()
-
xlabel()
-
ylabel()
-
zlabel()
例:
x = 0 : 0.1 : 2*pi;
y1 = sin(x);
y2 = exp(-x);
plot(x,y1,'--*', x,y2,':o');
legend('sin(t)', 'e^{-x}');
title("Function Plots of sin(t) and e^{-x}");
xlabel('t = 0 to 2\pi');
ylabel('values of sin(t) and e^{-x}');
title and label.jpg
?
?
-
\int_{0}^{2} ???定積分符號 上下界
-
text()
-
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]);
text.jpg
?
?
練習(xí):
t = linspace(1,2);
y = t .^ 2;
g = sin(2*pi*t);
plot(t,y,'-k', t,g,'o:r');
legend('t^2', 'sin(2\pi t)');
title("Mini Assignment #1");
xlabel('Time (ms)');
ylabel('f(t)');
exercise.jpg
?
?
?
Figure Adjustment(調(diào)整)
Snip20180213_5.png
http://www.mathworks.com/help/matlab/ref/figure-properties.html
?
-
Figure 's properties:
- Figure Object.png
-
Axes 's properties:
- Axes Object.png
-
Line 's properties:
- Line Object.png
-
Text 's properties
- Text Obejct.png
?
?
方案:
-
先找handle of object。
-
再獲取或者修改object的屬性复罐。
- 策略.png
?
操作步驟:
1. 獲取handle的方法:
-
get handle.png
?
2.1 查看某個(gè)object有哪些屬性沿侈?用get(handle)方法,會在terminal打印所有properties:
-
get(handle)市栗,查看所有properties.png
?
2.2 設(shè)置坐標(biāo)軸的范圍:
-
set(handle, property, value).png
?
2.3 設(shè)置坐標(biāo)軸字體大小缀拭、設(shè)置坐標(biāo)軸的標(biāo)記的內(nèi)容:
- set(handle, property, value).png
?
代碼:
x = linspace(0,3);
y = x .^ 2 .* sin(x);
line_sin = plot(x,y);
line_2 = line([3*pi/4,3*pi/4], [0, (3*pi/4) ^ 2 * sin(3*pi/4)]);
str = '$$ \int_{0}^{3\pi/4} x^2\sin(x) dx $$';
anno_text = text(0.18, 3, str, 'Interpreter', 'latex');
annotation('arrow', 'X', [0.33, 0.55], 'Y', [0.6, 0.35]); %整個(gè)版面:X:[0,1] Y:[1,0]
% 調(diào)整圖像屬性:
% get(line_sin); %查看Line有哪些屬性
set(line_sin, 'LineStyle','-', 'LineWidth',3, 'Color',[0,0.5,0.7]) %修改Line的屬性
% get(line_2);
% set(line_2, 'LineStyle', '--');
set(line_2, 'Color', [0,0,0]);
% get(anno_text);
set(anno_text, 'FontSize', 25);
% get(gca); %查看坐標(biāo)軸的所有屬性
set(gca, 'FontSize', 15);
set(gca, 'XLim', [0,pi]);
% XTick 和 XTickLabel 是對應(yīng)的:
set(gca, 'XTick', 0:pi/4:2*pi);
% set(gca, 'XTickLabel', 0:45:180);
set(gca, 'TickLabelInterpreter', 'TeX'); %改成字符型
set(gca, 'XTickLabel', {'0', '\pi/4', '\pi/2', '3\pi/4', '\pi'});
set(gca, 'YLim', [0,4.5]);
% delete(line_sin); %刪除某個(gè)圖像
- after_Adjustment.jpg
?
?
?
例子:修改 Line Object 的 Marker
x = rand(20,1);
line1 = plot(x, 'd-m');
% get(gca);
set(gca, 'XLim',[1,20], 'FontSize',15);
% get(line1);
set(line1, 'LineWidth',2, 'MarkerFaceColor','g', 'MarkerEdgeColor','k', 'MarkerSize',10);
- marker.jpg
?
?
?
Multiple Figure
figure
命令咳短,用來創(chuàng)建figure window.
例:
x = -10 : 0.1 : 10;
y1 = x.^2 - 8;
y2 = exp(x);
figure, plot(x,y1);
figure, plot(x,y2);
- Multiple_Figure.png
Be Careful using
gcf
、gca
handle.
gcf
蛛淋、gca
handle 指的是最后畫的這個(gè)figure咙好,前面的就找不到了。
?
?
?
Figure Position and Size :
- Position.png
?
?
?
?
一個(gè) Figure Window褐荷,畫許多不同的 small figure勾效,使用 subplot(m,n,1)
- subplot.png
m: num of rows
n: num of cols
?
?
x、y軸的比例:
-
axis normal: 自動比例
-
axis square: 方的
-
axis equal: data units are the same(最正確的比例)
-
axis equal tight: 最正確的比例切掉空余地方叛甫。
- axis_equal.png
?
?
-
打開/關(guān)閉 網(wǎng)格
-
打開/關(guān)閉 y軸的upper bound 和 x軸的upper bound(上邊框和右邊框)
-
打開/關(guān)閉 x层宫、y軸
- on/off.png
?
?
?
保存:
- saveas.png