原文:http://www.cnblogs.com/JCSU/articles/1443033.html
一、圖形控制
plot(x, y, 'CLM')
C:曲線的顏色(Colors)
L:曲線的格式(Line Styles)
M:曲線的線標(Markers)
?
x = 0:0.5:4*pi;? % x 向量的起始與結束元素為 0 及 4*pi, 0.5為各元素相差值
y = sin(x);
plot(x,y,'k:diamond') % 其中k代表黑色,:代表點線,而diamond則指定菱形為曲線的線標
?
plot 指令的曲線顏色
Plot指令的曲線顏色字串????? 曲線顏色
b????????????????????????????????? 藍色(Blue)
c????????????????????????????????? 青藍色(Cyan)
g????????????????????????????????? 綠色(Green)
k????????????????????????????????? 黑色(Black)
m???????????????????????????????? 紫黑色(Magenta)
r?????????????????????????????????? 紅色(Red)
w??????????????????????????????????白色
y????????????????????????????????? 黃色(Yellow)
?
plot 指令的曲線樣式
Plot指令的曲線樣式字串?????曲線樣式
-????????????????????????????????? 實線(默認值)
--????????????????????????????????虛線
: ?????????????????????????????? 點線
-.????????????????????????????????點虛線
?
plot 指令的曲線線標
Plot指令的曲線線標字串???? 曲線線標
O???????????????????????????????? 圓形
+???????????????????????????????? 加號
X???????????????????????????????? 叉號
*???????????????????????????????? 星號
.???????????????????????????????? 點號
^??????????????????????????????? 朝上三角形
V??????????????????????????????? 朝下三角形
>????????????????????????????????朝右三角形
<??????????????????????????????? 朝左三角形
square???????????????????????? 方形
diamond????????????????????? 菱形
pentagram?????????????????? 五角星形
hexagram??????????????????? 六角星形
None????????????????????????? 無符號(默認值)
?
二、圖軸控制
plot 指令會根據坐標點自動決定坐標軸范圍,也可以使用axis指令指定坐標軸范圍
使用語法:
axis([xmin, xmax, ymin, ymax])
xmin, xmax:指定 x 軸的最小和最大值
ymin, ymax:指定 y 軸的最小和最大值
?
x = 0:0.1:4*pi;
y = sin(x);
plot(x, y);
axis([-inf, inf, 0, 1]);? % 畫出正弦波 y 軸介于 0 和 1 的部份
?
指定坐標軸上的網格點(Ticks)
x = 0:0.1:4*pi;
plot(x, sin(x)+sin(3*x))
set(gca, 'ytick', [-1 -0.3 0.1 1]); % 在 y 軸加上網格點
grid on % 加上網格
gca:get current axis的簡稱,傳回目前使用中的坐標軸
?
將網格點的數字改為文字
x = 0:0.1:4*pi;
plot(x, sin(x)+sin(3*x))
set(gca, 'ytick', [-1 -0.3 0.1 1]);? % 改變網格點
set(gca, 'yticklabel', {'極小', '臨界值', '崩潰值', '極大'});? % 改變網格點的文字
grid on
?
在一個視窗中同時畫出四個圖
x = 0:0.1:4*pi;?
subplot(2, 2, 1); plot(x, sin(x));??% 左上角圖形
subplot(2, 2, 2); plot(x, cos(x));??% 右上角圖形
subplot(2, 2, 3); plot(x, sin(x).*exp(-x/5));?% 左下角圖形
subplot(2, 2, 4); plot(x, x.^2);??% 右下角圖形
?
長寬比(Aspect Ratio)
一般圖軸長寬比是視窗的長寬比, 可在axis指令后加不同的字串來修改
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 %長寬比例為 1
subplot(2, 2, 3); plot(x, y); axis equal %長寬比例不變,但兩軸刻度一致
subplot(2, 2, 4); plot(x, y); axis equal tight %兩軸刻度比例一致,且圖軸貼緊圖形
?
三、grid 和 box 指令
grid on ?畫出網格
grid off ?取消網格
box on?? 畫出圖軸的外圍長方形
box off ?取消圖軸的外圍長方形
?
給圖形和圖軸加說明文字
指令???????? 說明
title???????? 圖形的標題
xlabel?x??? 軸的說明
ylabel?y??? 軸的說明
zlabel?z??? 軸的說明
legend???? 多條曲線的說明
text???????? 在圖形中加入文字
gtext?????? 使用滑鼠決定文字的位置
?
subplot(1,1,1);?
x = 0:0.1:2*pi;?
y1 = sin(x);?
y2 = exp(-x);?
plot(x, y1, '--*', x, y2, ':o');?
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指令
text(x, y, string)
x、y :文字的起始座標位置
string :代表此文字
x = 0:0.1:2*pi;
plot(x, sin(x), x, cos(x));
text(pi/4, sin(pi/4),'\leftarrow sin(\pi/4) = 0.707');
text(5*pi/4, cos(5*pi/4),'cos(5\pi/4) = -0.707\rightarrow', 'HorizontalAlignment', 'right');
「HorizontalAlignment」及「right」將文字向右水平靠齊
?
?
四、各種二維繪圖指令
指令??????????????? 說明
errorbar?????????? 在曲線加上誤差范圍
fplot、ezplot???? 較精確的函數圖形
polar、ezpolar?? 極座標圖形
hist????????????? 直角座標直方圖(累計圖)
rose???????????? 極座標直方圖(累計圖)
compass???????羅盤圖
feather?????????羽毛圖
area???????????? 面積圖
stairs???????????階梯圖
已知資料的誤差范圍,用 errorbar 表示。以 y 坐標高度 20% 作為資料的誤差范圍
x = linspace(0,2*pi,30);?% 在 0 到 2 之間,等分取 30 個點
y = sin(x);
e = y*0.2;
errorbar(x,y,e)??% 圖形上加上誤差范圍 e
?
fplot 指令:對劇烈變化處進行較密集的取樣
fplot('sin(1/x)', [0.02 0.2]);?% [0.02 0.2]是繪圖范圍
?
theta = linspace(0, 2*pi);???
r = cos(4*theta);??
polar(theta, r);? % 進行極坐標繪圖
?
將 10000 個由 randn 產生的正規分布之隨機數分成 25 堆
x = randn(10000, 1);? % 產生 10000 個正規分布隨機數
hist(x, 25);? % 繪出直方圖,顯示 x 資料的分布情況和統計特性,數字 25 代表資料依大小分堆的堆數,即是指方圖內長條的個數
set(findobj(gca, 'type', 'patch'), 'edgecolor', 'w');% 將長條圖的邊緣設定成白色
?
試寫一函數 regpoly(n),其功能是畫出一個圓心在 (0, 0)、半徑為 1 的圓,并在圓內畫出一個內接正 n 邊形,其中一頂點位于 (0, 1)。
regpoly.m文件:
function regpoly(n)
vertices=[1];
for i=1:n
?step=2*pi/n;
?vertices=[vertices, exp(i*step*sqrt(-1))];
end
plot(vertices, '-o');
axis image
% 畫外接圓
hold on
theta=linspace(0, 2*pi);
plot(cos(theta), sin(theta), '-r');
hold off
axis image
?
一條參數式的曲線可由下列方程式表示:
x = sin(t), y = 1 - cos(t) + t/10
當 t 由 0 變化到 4*pi 時,請寫一個 MATLAB 的腳本 plotParam.m,畫出此曲線在 XY 平面的軌跡。
t = linspace(0, 4*pi);
x = sin(t);
y = 1-cos(t)+t/10;
plot(x, y, '-o');