1. 程式人生 > >matlab畫圖設定(座標軸、曲線、顏色)

matlab畫圖設定(座標軸、曲線、顏色)

a=linspace(1,2,10)

plot(a,'--pr','linewidth',1.5,'MarkerEdgeColor','r','MarkerFaceColor','m','MarkerSize',10)

legend('a','Location','best')

title('a','FontName','Times New Roman','FontWeight','Bold','FontSize',16)

xlabel('T','FontName','Times New Roman','FontSize',14)

ylabel('a','FontName','Times New Roman','FontSize',14,'Rotation',0)

axis auto equal

set(gca,'FontName','Times New Roman','FontSize',14)

1.曲線線型、顏色和標記點型別

plot(X1,Y1,LineSpec, …) 通過字串LineSpec指定曲線的線型、顏色及資料點的標記型別。

     線型            顏色        資料點標記型別

 識別符號  意義     識別符號意義     識別符號   意義

    -    實線     r   紅色        +     加號

    -. 點劃線     g   綠色        o     圓圈

   --    虛線     b   藍色        *     星號

    :    點線     c  藍綠色        .      點

                  m  洋紅色       x   交叉符號

                  y   黃色   square(或s) 方格

                  k   黑色  diamond(或d) 菱形

                  w   白色       ^  向上的三角形

                                 v  向下的三角形

                                 >  向左的三角形

                                 <  向右的三角形

                             pentagram(或p) 五邊形

                              hexagram(或h) 六邊形

2.設定曲線線寬、標記點大小,標記點邊框顏色和標記點填充顏色等。

plot(…,’Property Name’, Property Value, …)

Property Name 意義    選項

LineWidth 線寬    數值,如0.5,1等,單位為points

MarkerEdgeColor 標記點邊框線條顏色顏色字元,如’g’, ’b’等

MarkerFaceColor 標記點內部區域填充顏色顏色字元

MarkerSize 標記點大小   數值,單位為points

3.座標軸設定

範圍設定:

a. axis([xmin xmax ymin ymax])設定座標軸在指定的區間

b. axis auto 將當前繪圖區的座標軸範圍設定為MATLAB自動調整的區間

c. axis manual 凍結當前座標軸範圍,以後疊加繪圖都在當前座標軸範圍內顯示

d. axis tight 採用緊密模式設定當前座標軸範圍,即以使用者資料範圍為座標軸範圍比例:

a. axis equal 等比例座標軸

b. axis square 以當前座標軸範圍為基礎,將座標軸區域調整為方格形

c. axis normal 自動調整縱橫軸比例,使當前座標軸範圍內的圖形顯示達到最佳效果

範圍選項和比例設定可以聯合使用,預設的設定為axis auto normal

4.座標軸刻度設定

set(gca, ’XTick’, [0 1 2]) X座標軸刻度資料點位置

set(gca,'XTickLabel',{'a','b','c'}) X座標軸刻度處顯示的字元

set(gca,'FontName','Times New Roman','FontSize',14)設定座標軸刻度字型名稱,大小

‘FontWeight’,’bold’ 加粗 ‘FontAngle’,’italic’ 斜體

對字型的設定也可以用在title, xlabel, ylabel等中

5.圖例

legend('a','Location','best') 圖例位置放在最佳位置

6.更多的設定可以在繪圖視窗中開啟繪圖工具,Inspector… 中查詢

Various line types, plot symbols and colors may be obtained with

PLOT(X,Y,S) where S is a character string made from one element

from any or all the following 3 columns:

          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

          w     white       v     triangle (down)

                           ^     triangle (up)

                           <     triangle (left)

                           >     triangle (right)

                           p     pentagram

                           h     hexagram

在使用Matlab時,經常需要將得到的數值表達成二維或三維影象。

plot(vector1,vector2)可以用來畫兩個向量的二維圖,例如

x=1:0.1:2*pi;

plot(x,sin(x))可以畫正弦函式在0-2pi的上的影象。

plot函式可以接一些引數,來改變所畫影象的屬性(顏色,影象元素等)。下面是一些屬性的說明

           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

例如,plot(x,y,'.r')表示用點來畫圖,點的顏色是紅色。

plot函式可以接一些引數,來改變所畫影象的屬性(顏色,影象元素等)。下面是一些屬性的說明

           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

    Example

       x = -pi:pi/10:pi;

       y = tan(sin(x)) - sin(tan(x));

       plot(x,y,'--rs','LineWidth',2,...

                       'MarkerEdgeColor','k',...

                       'MarkerFaceColor','g',...

                       'MarkerSize',10)

           xlabel('x');

           ylabel('y');

·         用Matlab畫圖時,有時候需要對各種圖示進行標註,例如,用“+”代表A的運動情況,“*”代表B的運動情況。

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) 來指定圖例標識框的位置

這些是Matlab help檔案。後面一段是對應的翻譯和說明

        '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

        'North'             圖例標識放在圖頂端

        'South'            圖例標識放在圖底端

        'East'               圖例標識放在圖右方

        'West'              圖例標識放在圖左方

        'NorthEast'       圖例標識放在圖右上方(預設)

        'NorthWest      圖例標識放在圖左上方

        'SouthEast'      圖例標識放在圖右下角

        'SouthWest'     圖例標識放在圖左下角

(以上幾個都是將圖例標識放在框圖內)

        'NorthOutside'          圖例標識放在圖框外側上方

        'SouthOutside'         圖例標識放在圖框外側下方

        'EastOutside'           圖例標識放在圖框外側右方

        'WestOutside'          圖例標識放在圖框外側左方

        'NorthEastOutside'   圖例標識放在圖框外側右上方

        'NorthWestOutside' 圖例標識放在圖框外側左上方

        'SouthEastOutside'   圖例標識放在圖框外側右下方

        'SouthWestOutside' 圖例標識放在圖框外側左下方

(以上幾個將圖例標識放在框圖外)

        'Best'                      圖示標識放在圖框內不與圖衝突的最佳位置

        'BestOutside'           圖示標識放在圖框外使用最小空間的最佳位置

還是用上面的例子

legend('sin','cos','location','northwest')可以將標識框放置在圖的左上角。

    Examples:

        x = 0:.2:12;

        plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));

        legend('First','Second','Third');

        legend('First','Second','Third','Location','NorthEastOutside')

        b = bar(rand(10,5),'stacked'); colormap(summer); hold on

        x = plot(1:10,5*rand(10,1),'marker','square','markersize',12,...

                 'markeredgecolor','y','markerfacecolor',[.6 0 .6],...

                 'linestyle','-','color','r','linewidth',2); hold off

        legend([b,x],'Carrots','Peas','Peppers','Green Beans',...

                  'Cucumbers','Eggplant')