MATLAB--基本绘图函数
MATLAB基本繪圖函數(shù)
plot: x軸和y軸均為線性刻度(Linear scale)
loglog: x軸和y軸均為對數(shù)刻度(Logarithmic scale)
semilogx: x軸為對數(shù)刻度,y軸為線性刻度
semilogy: x軸為線性刻度,y軸為對數(shù)刻度
====================================================
x=linspace(0,2*pi,100);plot(x,sin(x),'co',x,cos(x),'g*')若要畫出多條曲線,只需將座標(biāo)對依次放入plot函數(shù)即可:
plot(x, sin(x), x, cos(x));
若要改變顏色,在座標(biāo)對後面加上相關(guān)字串即可:
plot(x, sin(x), ‘c’, x, cos(x), ‘g’);
若要同時(shí)改變顏色及圖線型態(tài)(Line style),也是在座標(biāo)對後面加上相
關(guān)字串即可:
plot(x, sin(x), ‘co’, x, cos(x), ‘g*’);
====================================================
小整理:plot繪圖函數(shù)的叁數(shù)
字元 顏色 字元 圖線型態(tài)
y 黃色 . 點(diǎn)
k 黑色 o 圓
w 白色 x x
b 藍(lán)色 + +
g 綠色 * *
r 紅色 - 實(shí)線
c 亮青色 : 點(diǎn)線
m 錳紫色 -. 點(diǎn)虛線
– 虛線
///
圖形完成後,我們可用axis([xmin,xmax,ymin,ymax])函數(shù)來調(diào)整圖軸的范
圍:
axis([0, 6, -1.2, 1.2]);
此外,MATLAB也可對圖形加上各種注解與處理:
xlabel(‘Input Value’); % x軸注解
ylabel(‘Function Value’); % y軸注解
title(‘Two Trigonometric Functions’); % 圖形標(biāo)題
legend(‘y = sin(x)’,‘y = cos(x)’); % 圖形注解
grid on; % 顯示格線
我們可用subplot來同時(shí)畫出數(shù)個(gè)小圖形於同一個(gè)視窗之中:
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));
MATLAB還有其他各種二維繪圖函數(shù),以適合不同的應(yīng)用,詳見下表。
====================================================
小整理:其他各種二維繪圖函數(shù)
bar 長條圖
x=1:10 y=rand(size(x)) bar(x,y)errorbar 圖形加上誤差范圍 如果已知資料的誤差量,就可用errorbar來表示。下例以單位標(biāo)準(zhǔn)差來做
x=linspace(0,2*pi,30) y=sin(x) e=std(y)*ones(size(x)); errorbar(x,y,e)資料的誤差量:
fplot 較精確的函數(shù)圖形
***對於變化劇烈的函數(shù),可用fplot來進(jìn)行較精確的繪圖,會(huì)對劇烈變化處進(jìn)行較密集的取樣,如下例:
fplot(‘sin(1/x)’, [0.02 0.2]); % [0.02 0.2]是繪圖范圍
fplot('sin(1/x)',[0.02,0.2])polar 極座標(biāo)圖
theta=linspace(0,2*pi); r=cos(4*theta) polar(theta,r)hist 累計(jì)圖
x=randn(1000,1) hist(x);rose 極座標(biāo)累計(jì)圖
stairs 階梯圖
stem 針狀圖
x=linspace(0,10,50) y=sin(x).*exp(-x/3) stem(x,y)fill 實(shí)心圖
feather 羽毛圖
theta=linspace(0,2*pi,20) z=cos(theta)+i*sin(theta) feather(z)compass 羅盤圖
theta=linspace(0,2*pi,20) z=cos(theta)+i*sin(theta) compass(z)quiver 向量場圖
3.基本XYZ立體繪圖命令
在科學(xué)目視表示(Scientific visualization)中,三度空間的立體圖是
一個(gè)非常重要的技巧。本章將介紹MATLAB基本XYZ三度空間的各項(xiàng)繪圖命
令。
mesh和plot是三度空間立體繪圖的基本命令,mesh可畫出立體網(wǎng)狀圖,
plot則可畫出立體曲面圖,兩者產(chǎn)生的圖形都會(huì)依高度而有不同顏色。下
列命令可畫出由函數(shù) 形成的立體網(wǎng)狀圖:
x=linspace(-2, 2, 25); % 在x軸上取25點(diǎn)
y=linspace(-2, 2, 25); % 在y軸上取25點(diǎn)
[xx,yy]=meshgrid(x, y); % xx和yy都是21x21的矩陣
zz=xx.*exp(-xx.2-yy.2); % 計(jì)算函數(shù)值,zz也是21x21的矩陣
mesh(xx, yy, zz); % 畫出立體網(wǎng)狀圖
surf和mesh的用法類似:
x=linspace(-2, 2, 25); % 在x軸上取25點(diǎn)
y=linspace(-2, 2, 25); % 在y軸上取25點(diǎn)
[xx,yy]=meshgrid(x, y); % xx和yy都是21x21的矩陣
zz=xx.*exp(-xx.2-yy.2); % 計(jì)算函數(shù)值,zz也是21x21的矩陣
surf(xx, yy, zz); % 畫出立體曲面圖
為了方便測試立體繪圖,MATLAB提供了一個(gè)peaks函數(shù),可產(chǎn)生一個(gè)凹凸有
致的曲面,包含了三個(gè)局部極大點(diǎn)及三個(gè)局部極小點(diǎn),其方程式為:
要畫出此函數(shù)的最快方法即是直接鍵入peaks:
peaks
z = 3*(1-x).2.*exp(-(x.2) - (y+1).^2) …
-
10*(x/5 - x.^3 - y.5).*exp(-x.2-y.^2) …
-
1/3*exp(-(x+1).^2 - y.^2)
我們亦可對peaks函數(shù)取點(diǎn),再以各種不同方法進(jìn)行繪圖。meshz可將曲面
加上圍裙:
[x,y,z]=peaks;
meshz(x,y,z);
axis([-inf inf -inf inf -inf inf]);
waterfall可在x方向或y方向產(chǎn)生水流效果:
[x,y,z]=peaks;
waterfall(x,y,z);
axis([-inf inf -inf inf -inf inf]);
下列命令產(chǎn)生在y方向的水流效果:
[x,y,z]=peaks;
waterfall(x’,y’,z’);
axis([-inf inf -inf inf -inf inf]);
meshc同時(shí)畫出網(wǎng)狀圖與等高線:
[x,y,z]=peaks;
meshc(x,y,z);
axis([-inf inf -inf inf -inf inf]);
surfc同時(shí)畫出曲面圖與等高線:
[x,y,z]=peaks;
surfc(x,y,z);
axis([-inf inf -inf inf -inf inf]);
contour3畫出曲面在三度空間中的等高線:
contour3(peaks, 20);
axis([-inf inf -inf inf -inf inf]);
contour畫出曲面等高線在XY平面的投影:
contour(peaks, 20);
plot3可畫出三度空間中的曲線:
t=linspace(0,20*pi, 501);
plot3(t.*sin(t), t.*cos(t), t);
亦可同時(shí)畫出兩條三度空間中的曲線:
t=linspace(0, 10*pi, 501);
plot3(t.*sin(t), t.*cos(t), t, t.*sin(t), t.*cos(t), -t);
總結(jié)
以上是生活随笔為你收集整理的MATLAB--基本绘图函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab函数作图格式,Matlab绘
- 下一篇: 智能雷达感应人体存在,照明雷达技术应用,