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