Matlab繪圖基本控制命令

來源:互聯網
上載者:User

標籤:

圖形的控制與表現 (Figure control and representation)

MATLAB提供的用於圖形控制的函數和命令:
   axis:  人工選擇座標軸尺寸.
    clf:清圖形視窗.
 ginput: 利用滑鼠的十字準線輸入.
   hold: 保持圖形.
    shg:顯示圖形視窗.
subplot: 將圖形視窗分成N塊子視窗。

1.圖形視窗(figure window)
(1). 圖形視窗的建立和選擇(Creating and selecting of figure window)
     figure(n):用於為當前的繪圖建立圖形視窗,每運行一次figure就會建立一個新的圖形視窗,n表示第n個

                視窗,如果視窗定義了控制代碼,也可以用figure(h)將控制代碼h的視窗作為當前視窗。
          clf :用於清除當前圖形視窗中的內容。
          shg :用於顯示當前圖形視窗。


(2). 在一個圖形視窗中繪製多個子圖形(Drawing several subfigures in a single window)
subplot(m,n,p):把視窗分成m×n個小視窗,並把第p個視窗當作當前視窗。
例:將4 個圖形顯示在同一個圖形視窗中。
    t=0:pi/20:2*pi; [x,y]=meshgrid(t);
    subplot(2,2,1); plot(sin(t),cos(t)); axis equal
    subplot(2,2,2); z=sin(x)+cos(y); plot(t,z); axis([0 2*pi –2 2])
    subplot(2,2,3); z=sin(x).*cos(y); plot(t,z); axis([0 2*pi –1 1])
    subplot(2,2,4); z=sin(x).^2-cos(y).^2; plot(t,z); axis([0 2*pi –1 1])


(3). 在一個已有的圖形上繪圖(Drawing a figure on the figure was existed) 
      hold on :在一個已有的圖形上繼續繪圖;

      hold off: 命令結束繼續繪圖。
例:將peaks函數的等高線圖與偽彩色畫在一起。
    [x,y,z]=peaks;          %產生雙變數數組
    contour(x,y,z,20,‘k‘)        %繪製等高線
    hold on
    pcolor(x,y,z)             %繪製偽彩色圖
    shading interp          %表面色彩渲染
    hold off


2.座標軸控制命令(Axis control commands)
   控制座標性質的axis函數的多種調用格式:
axis(xmin xmax ymin ymax)  :指定二維圖形x和y軸的刻度範圍,
axis auto                  :設定座標軸為自動刻度(預設值)
axis manual(或axis(axis)):保持刻度不隨資料的大小而變化
axis tight                 :以資料的大小為座標軸的範圍
axis ij                    :設定座標軸的原點在左上方,i為縱座標,j為橫座標
axis xy                    :使座標軸回到直角座標系
axis equal                 :使座標軸刻度增量相同
axis square                :使各座標軸長度相同,但刻度增量未必相同
axis normal                :自動調節軸與資料的外表比例,使其他設定失效
axis off                   :使座標軸消隱
axis on                    :顯現座標軸


(1) 座標軸的範圍(Domain of coordinates axis)
二維圖形座標軸範圍在預設狀態下是根據資料的大小自動化佈建的,如欲改變,可利用axis(xmin xmax ymin ymax),函數來定義。
例: 定義座標軸範圍對觀察圖形的影響。
x=0:.01:pi/2; figure(1); plot(x,tan(x),‘-ro‘)     %ymax=tan(1.57),而其他資料都很小,結果將
%使圖形難於進行觀察和判斷。
figure(2); plot(x,tan(x),‘-ro‘); axis([0, pi/2,0,5])   %對座標軸的範圍進行控制就可得到較滿意的繪圖結果


(2) 顯示比例對繪圖結果的影響(Effect of display scaling on plotting results)
例:比較(Default, axis square, axis equal, axis tight)幾種不同的顯示方式的顯示效果。
t=0:pi/20:2*pi; figure(1);
subplot(2,1,1); plot(sin(t),2*cos(t)); grid on     %預設狀態下的圖形比例
subplot(2,1,2); plot(sin(t),2*cos(t)); axis square; grid on    %正方形的顯示比例
figure(2)
subplot(1,2,1); plot(sin(t),2*cos(t)) ; axis equal; grid on           %具有相等的刻度比例
subplot(1,2,2); plot(sin(t),2*cos(t)); axis tight ; grid on              %緊縮形式

3.圖形標註(Marking on the figure):MATLAB的圖形標註方法(表 6—7)
 title :標題,
xlabel :x軸標註,
ylabel :y軸標註,
  text :任意定位的標註                      
 gtext :滑鼠定位標註,
legent :標註圖例


圖形標註可以使用字母,數字,漢字或按規定的方法表示希臘字母。如:\pi表示π,\leq表示≤,\rm表示後面的字恢複為正體字,\it表示斜體字,FontSize表示字型的大小, FontName表示字型的類型等。
可以使用圖形視窗的Insert菜單,也可以使用屬性編輯器,還可以使用函數輸入的方法加標註,以下介紹相關函數的使用方法。


(1). 加註座標軸標識和圖形標題(Add axis labels and title of figure)
加註座標軸標識:xlabel(‘s’), ylabel(‘s’)
      圖形標題: title(‘s’)
例:加註座標軸標示和圖形標題。
    t=0:pi/100:2*pi;y=sin(t);
    plot(t,y)
    axis([0 2*pi,-1 1])
    xlabel(‘0 \leq \itt \rm \leq \pi‘,‘FontSize‘,16)
    ylabel(‘sin(t)‘,‘FontSize‘,20)
    title(‘正弦函數圖形‘,‘FontName‘,‘隸書‘,‘FontSize‘,20)


(2). 圖中加註文本(Add text in the figure)
text(x,y,’字串’)
例:在中加語句。
    t=0:pi/100:2*pi;
    y=sin(t);
    plot(t,y)
    axis([0 2*pi,-1 1])                               
    xlabel(‘0 \leq \itt \rm \leq \pi‘,‘FontSize‘,16)
    ylabel(‘sin(t)‘,‘FontSize‘,20)
    title(‘正弦函數圖形‘,‘FontName‘,‘隸書‘,‘FontSize‘,20)
    text(3*pi/4,sin(3*pi/4),‘\leftarrowsin(t)=0.707‘, ‘FontSize‘,16)
    text(pi,sin(pi),‘\leftarrowsin(t)=0‘, ‘FontSize‘,16)
    text(5*pi/4,sin(5*pi/4),‘sin(t)=-0.707\rightarrow‘,‘FontSize‘,16,...

         ‘HorizontalAlignment‘,‘right‘)
句中:
leftarrow           表示加一個向左的箭頭
rightarrow          表示加一個向右的箭頭
HorizontalAlignment 表示靠右對齊水平排列


gtext(‘字串‘): 在圖形視窗上用滑鼠直接在指定的位置上加註文本。

例:
    t=0:pi/100:2*pi;
    y=sin(t);
    plot(t,y)
    axis([0 2*pi,-1 1])
    xlabel(‘0 \leq \itt \rm \leq \pi‘,‘FontSize‘,16)
    ylabel(‘sin(t)‘,‘FontSize‘,20)
    title(‘正弦函數圖形‘,‘FontName‘,‘隸書‘,‘FontSize‘,20)
    gtext(‘MATLAB‘)


(3). 指定TeX字元
例:在標題中指定TeX字元
    t=0:pi/100:2*pi;
    alpha=-0.8;
    beta=15;
    y=sin(beta*t).*exp(alpha*t);
    plot(t,y)
    title(‘{\itAe}^{-\it\alpha\itt}sin\it\beta{\itt}\it\alpha<<\it\beta‘)
    xlabel(‘時間\mus.‘),
    ylabel(‘幅值‘)
在title中的字串表現的是 Aeαt   sinβt   α<<β
斜體Ae 上標斜體αt  斜體βt  斜體α  斜體β


(4). 在圖形中添加圖例框(Add legend in the figure)
legend(字串1,字串2,…)
例:在當前圖形中添加圖例說明。
    x=0:pi/10:2*pi;
    y1=sin(x);
    y2=0.6*sin(x);
    y3=0.3*sin(x);
    plot(x,y1,x,y2,‘-o‘,x,y3,‘-*‘)
    legend( ‘曲線1‘,‘曲線2‘,‘曲線3‘)
    legend(‘boxoff‘)
legend函數的其他功能見(表 6—8)

4. 圖線形式和顏色(Style and color of plot)
(1). 圖線的形式: (style of plot)

  四種線形: 實線‘-‘,虛線‘--‘, 點線‘:‘,點劃線‘-‘.
標記點類型:點‘.‘, 圓‘o‘, 加號‘+‘, 星號‘*‘, x符號‘x‘, 方形‘s‘, 菱形‘d‘, hexagram ‘h‘

            上三角△ ‘^‘, 下三角▽ ‘v‘, 左三角‘<‘, 右三角‘>‘, 正五邊形‘p‘

 

命令:plot(x,y,‘—‘), plot(x1,y1,‘:’,x2,y2,‘*‘)
例1:選擇不同的線形繪圖。    
    t=0:pi/100:2*pi; y=sin(t); y2=sin(t-0.25); y3=sin(t-0.5);
    plot(t,y,‘-‘,t,y2,‘-‘,t,y3,‘:‘)
例2:選擇不同的標記點繪圖。
   t=0:pi/20:2*pi; x=t.^3; y=sin(t); plot(x,y,‘o‘)


(2). 線的顏色(color of plot)

可選顏色: 紅r,綠g, 藍b, 黃y, 粉紅m, 青c, 黑k.
例:t=0:pi/20:2*pi;
    y=sin(t); plot(x,y,‘r‘), plot(x,y,‘g+‘)


(3). 圖線的其他屬性(other characters of plot)

設定圖線的寬度  : ‘LineWidth‘

標記點的邊緣顏色: ‘MarkerEdgeColor‘

填充顏色        : ‘MarkerFaceColor‘

標記點的大小    : ‘MarkerSize‘

例: 設定圖線的線形、顏色、寬度、標記點的顏色及大小。
    t=0:pi/20:pi; y=sin(4*t).*sin(t)/2;
    plot(t,y,‘-bs‘,‘LineWidth‘,2,‘MarkerEdgeColor‘,‘k‘, ‘MarkerFaceColor‘, ‘y‘,‘MarkerSize‘,10);

Matlab繪圖基本控制命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.