matlab——二維繪圖函數(shù)及部分參數(shù)
2009-10-21 10:02
MATLAB基本xy平面繪圖命令
plot: x軸和y軸均為線性刻度(Linear scale)
loglog: x軸和y軸均為對數(shù)刻度(Logarithmic scale)
semilogx: x軸為對數(shù)刻度,y軸為線性刻度
semilogy: x軸為線性刻度,y軸為對數(shù)刻度
注:
若要畫出多條曲線,只需將座標對依次放入plot函數(shù)即可:
plot(x, sin(x), x, cos(x));
若要改變顏色,在座標對後面加上相關(guān)字串即可:
plot(x, sin(x), 'c', x, cos(x), 'g');
若要同時改變顏色及圖線型態(tài)(Line style),也是在座標對後面加上相關(guān)字串即可:
plot(x, sin(x), 'co', x, cos(x), 'g*');
axis([xmin,xmax,ymin,ymax])函數(shù)可以調(diào)整圖軸的范圍:
axis([0, 6, -1.2, 1.2]);
補充:下面是一些參數(shù)的說明
b blue(藍色) . point(點) - solid(實線)
g green(綠色) o circle(圓圈) : dotted(點線)
r red(紅色) x x-mark(叉號) -. dashdot (點畫線)
c cyan(墨綠色) + plus(加號) -- dashed(虛線)
m magenta(紫紅色) * star(星號) (none) no line
y yellow(黃色) s square(正方形)
k black(黑色) d diamond(菱形)
v triangle (down)(下三角形)
^ triangle (up)(上三角形)
< triangle (left)(左三角形)
> triangle (right)(右三角形)
p pentagram(五角星)
h hexagram(六芒星)
此外,MATLAB也可對圖形加上各種注解與處理:
xlabel('Input Value'); % x軸注解
ylabel('Function Value'); % y軸注解
title('Two Trigonometric Functions'); % 圖形標題
legend('y = sin(x)','y = cos(x)'); % 圖形注解
grid on; % 顯示格線(反之為grid off)
hold on; % 保持圖形(反之為hold off)
我們可用subplot來同時畫出數(shù)個小圖形於同一個視窗之中:
subplot(2,2,1); plot(x, sin(x));
subplot(2,2,2); plot(x, cos(x));
subplot(2,2,3); plot(x, sinh(x));
subplot(2,2,4); plot(x, cosh(x));
====================================================
其他各種二維繪圖函數(shù)
bar 長條圖(適合資料點數(shù)量不多的情況)
errorbar 圖形加上誤差范圍(如果已知資料的誤差量,就可用errorbar來表示): errorbar(x,y,e); % e是誤差量
fplot 較精確的函數(shù)圖形(對于變化劇烈的函數(shù),可用fplot來進行較精確的繪圖,會對劇烈變化處進行較密集的取樣):fplot('sin(1/x)', [0.02 0.2]); % [0.02 0.2]是繪圖范圍
polar 極坐標圖
hist 累計圖(對于大量的資料,可用hist來顯示資料的分布情況和統(tǒng)計特性)
rose 極座標累計圖
stairs 階梯圖(將資料點視為多邊行頂點,并將此多邊行涂上顏色)
stem 針狀圖(常被用來繪制數(shù)位訊號)
fill 實心圖
feather 羽毛圖(將每一個資料點視復(fù)數(shù),并以箭號畫出)
compass 羅盤圖(和feather很接近,只是每個箭號的起點都在圓點)
quiver 向量場圖
====================================================
legend函數(shù)的基本用法
LEGEND(string1,string2,string3, ...)
分別將字符串1、字符串2、字符串3……標注到圖中,每個字符串對應(yīng)的圖標為畫圖時的圖標。
例如:
plot(x,sin(x),'.b',x,cos(x),'+r')
legend('sin','cos')這樣可以把"."標識為'sin',把"+"標識為"cos"
還可以用LEGEND(...,'Location',LOC) 來指定圖例標識框的位置,下面是LOC內(nèi)容的說明
將圖例標識放在框圖內(nèi):
'North' inside plot box near top(圖例標識放在圖頂端)
'South' inside bottom(圖例標識放在圖底端)
'East' inside right(圖例標識放在圖右方)
'West' inside left(圖例標識放在圖左方)
'NorthEast' inside top right (default)(圖例標識放在圖右上方(默認))
'NorthWest inside top left(圖例標識放在圖左上角)
'SouthEast' inside bottom right(圖例標識放在圖右下角)
'SouthWest' inside bottom left(圖例標識放在圖左下角)
將圖例標識放在框圖外:
'NorthOutside' outside plot box near top(圖例標識放在圖框外側(cè)上方)
'SouthOutside' outside bottom(圖例標識放在圖框外側(cè)下方)
'EastOutside' outside right(圖例標識放在圖框外側(cè)右方)
'WestOutside' outside left(圖例標識放在圖框外側(cè)左方)
'NorthEastOutside' outside top right(圖例標識放在圖框外側(cè)右上方)
'NorthWestOutside' outside top left(圖例標識放在圖框外側(cè)左上方)
'SouthEastOutside' outside bottom right(圖例標識放在圖框外側(cè)右下方)
'SouthWestOutside' outside bottom left(圖例標識放在圖框外側(cè)左下方)
其他:
'Best' least conflict with data in plot(圖標標識放在圖框內(nèi)不與圖沖突的最佳位置)
'BestOutside' least unused space outside plot(圖標標識放在圖框外使用最小空間的最佳位置)