1. 程式人生 > >Matlab繪圖(二維)

Matlab繪圖(二維)

1、plot

1)、plot(x) —— 預設自變數繪圖格式,x為向量, 以x元素值為縱座標,以相應元素下標為橫座標繪圖

2)、plot(x,y) —— 基本格式,以y(x)的函式關係作出直角座標圖,如果y為n×m的矩陣,則以x 為自變數,作出m條曲線 3)、plot(x1,y1,x2,y2) —— 多條曲線繪圖格式

4)、plot(x,y,’s’) —— 開關格式,開關量字串s設定曲線顏色和繪圖方式,使用顏色字串的前1~3個字母,如yellow—yel表示等。或plot(x1,y1,’s1’,x2,y2,’s2’,…)

                              

plot(t,y,'r-',t,y1,'g:',t,y2,'b*')

                           

2、子圖:subplot

3、多視窗繪圖:figure

figure(n) —— 建立視窗函式,n為窗 口順序號。 t=0:pi/100:2*pi; y=sin(t);y1=sin(t+0.25);y2=sin(t+0.5); plot(t,y) —— 自動出現第一個視窗 figure(2) plot(t,y1) —— 在第二視窗繪圖 figure(3) plot(t,y2) ——在第三視窗繪圖

4、給圖加註釋

將標題、座標軸標記、網格線及文字注 釋加註到圖形上,這些函式為: title —— 給圖形加標題 xlable —— 給x軸加標註 ylable —— 給y軸加標註 text —— 在圖形指定位置加標註 gtext —— 將標註加到圖形任意位置 grid on(off) —— 開啟、關閉座標網格線 legend —— 新增圖例 axis —— 控制座標軸的刻度

例:t=0:0.1:10 y1=sin(t);y2=cos(t);plot(t,y1,'r',t,y2,'b--'); x=[1.7*pi;1.6*pi]; y=[-0.3;0.8]; s=['sin(t)';'cos(t)']; text(x,y,s); title('正弦和餘弦曲線'); legend('正弦','餘弦') xlabel('時間t'),ylabel('正弦、餘弦') grid axis square

axis的用法還有: axis([xmin xmax ymin ymax]) —— 用行向量中給出的值設定座標軸的最大和最小值。 如axis ([-2 2 0 5]) axis(equal) —— 將兩座標軸設為相等 axis on(off) —— 顯示和關閉座標軸的標記、標誌 axis auto —— 將座標軸設定返回自動預設值

5、其他繪圖

bar –––– 繪製直方圖 polar –––– 繪製極座標圖 hist –––– 繪製統計直方圖 stairs –––– 繪製階梯圖 stem –––– 繪製火柴桿圖 rose –––– 繪製統計扇形圖 comet –––– 繪製彗星曲線

errorbar –––– 繪製誤差棒圖 compass –––– 複數向量圖(羅盤圖) feather –––– 複數向量投影圖(羽毛圖) quiver –––– 向量場圖 area –––– 區域圖 pie –––– 餅圖 convhull –––– 凸殼圖 scatter –––– 離散點圖

例,繪製階梯曲線 x=0:pi/20:2*pi;y=sin(x);stairs(x,y)

例:階梯繪圖 h2=[1 1;1 -1];h4=[h2 h2;h2 -h2]; h8=[h4 h4;h4 -h4];t=1:8; subplot(8,1,1);stairs(t,h8(1,:));axis('off') subplot(8,1,2);stairs(t,h8(2,:));axis('off') subplot(8,1,3);stairs(t,h8(3,:));axis('off') subplot(8,1,4);stairs(t,h8(4,:));axis('off') subplot(8,1,5);stairs(t,h8(5,:));axis('off') subplot(8,1,6);stairs(t,h8(6,:));axis('off') subplot(8,1,7);stairs(t,h8(7,:));axis('off') subplot(8,1,8);stairs(t,h8(8,:));axis('off')

h2=[1 1;1 -1];h4=[h2 h2;h2 -h2]; h8=[h4 h4;h4 -h4]; t=1:8; for i=1:8 subplot(8,1,i); stairs(t,h8(i,:)) axis('off') end