天天看點

matlab——二維繪圖函數及部分參數

matlab——二維繪圖函數及部分參數

2009-10-21 10:02

MATLAB基本xy平面繪圖指令

plot: x軸和y軸均為線性刻度(Linear scale)

loglog: x軸和y軸均為對數刻度(Logarithmic scale)

semilogx: x軸為對數刻度,y軸為線性刻度

semilogy: x軸為線性刻度,y軸為對數刻度

注:

若要畫出多條曲線,隻需将座标對依次放入plot函數即可:

plot(x, sin(x), x, cos(x));

若要改變顔色,在座标對後面加上相關字串即可:

plot(x, sin(x), 'c', x, cos(x), 'g');

若要同時改變顔色及圖線型态(Line style),也是在座标對後面加上相關字串即可:

plot(x, sin(x), 'co', x, cos(x), 'g*');

axis([xmin,xmax,ymin,ymax])函數可以調整圖軸的範圍:

axis([0, 6, -1.2, 1.2]);

補充:下面是一些參數的說明

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來同時畫出數個小圖形於同一個視窗之中:

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));

====================================================

其他各種二維繪圖函數

bar 長條圖(适合資料點數量不多的情況)

errorbar 圖形加上誤差範圍(如果已知資料的誤差量,就可用errorbar來表示): errorbar(x,y,e); % e是誤差量

fplot 較精确的函數圖形(對于變化劇烈的函數,可用fplot來進行較精确的繪圖,會對劇烈變化處進行較密集的取樣):fplot('sin(1/x)', [0.02 0.2]); % [0.02 0.2]是繪圖範圍

polar 極坐标圖

hist 累計圖(對于大量的資料,可用hist來顯示資料的分布情況和統計特性)

rose 極座标累計圖

stairs 階梯圖(将資料點視為多邊行頂點,并将此多邊行塗上顔色)

stem 針狀圖(常被用來繪制數位訊号)

fill 實心圖

feather 羽毛圖(将每一個資料點視複數,并以箭号畫出)

compass 羅盤圖(和feather很接近,隻是每個箭号的起點都在圓點)

quiver 向量場圖

====================================================

legend函數的基本用法

LEGEND(string1,string2,string3, ...)

分别将字元串1、字元串2、字元串3……标注到圖中,每個字元串對應的圖示為畫圖時的圖示。

例如:

plot(x,sin(x),'.b',x,cos(x),'+r')

legend('sin','cos')這樣可以把"."辨別為'sin',把"+"辨別為"cos"

還可以用LEGEND(...,'Location',LOC) 來指定圖例辨別框的位置,下面是LOC内容的說明

将圖例辨別放在框圖内:

'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(圖例辨別放在圖框外側上方)

'SouthOutside'       outside bottom(圖例辨別放在圖框外側下方)

'EastOutside'        outside right(圖例辨別放在圖框外側右方)

'WestOutside'        outside left(圖例辨別放在圖框外側左方)

'NorthEastOutside'   outside top right(圖例辨別放在圖框外側右上方)

'NorthWestOutside'   outside top left(圖例辨別放在圖框外側左上方)

'SouthEastOutside'   outside bottom right(圖例辨別放在圖框外側右下方)

'SouthWestOutside'   outside bottom left(圖例辨別放在圖框外側左下方)

其他:

'Best'               least conflict with data in plot(圖示辨別放在圖框内不與圖沖突的最佳位置)

'BestOutside'        least unused space outside plot(圖示辨別放在圖框外使用最小空間的最佳位置)

繼續閱讀