【MATLAB】調整legend的大小位置

源地址:
數組

http://hi.baidu.com/csudeng/item/12437d10424e6f24f6625cc9ide


MATLAB中plot命令繪圖微調的幾個註記字體

一、MATLAB如何從硬盤讀取文件。對象

二、如何微調subplot子圖的位置。ci

三、plot命令繪曲線時,曲線上的標誌如何調整大小。get

四、座標軸的調整。it

六、座標標題中如何標上標。io

七、如何調整圖示(legend)的位置。ast


%----------------------------------class

% 這裏要畫一個2*2共4幅子圖。先將第1個子圖的位置調整。

h = subplot( 2, 2, 1); % 先讓MATLAB默 認繪製第1幅子圖,h是子圖1的句柄

po = get( h, 'Position' );   %       get命令從句柄h中獲取'Position'的內容,返回一個含4個元素的一維數組放到po中。這4個元 素分別是子圖1的left, bottom, width, height。

subplot( 'Position', [po(1)+0.03, po(2)-0.03, po(3), po(4)]);  子圖1的新位置能夠這樣調整

%----------------------------------


hold on;

axis([0 13 -3 2]);

set( gca, 'XTick', [1:12] );    gca表示當前對象句柄,set命令分別對當前對象(即子圖1)設置座標軸XTick和YTick屬性。這 兩個屬性分別表示了座標軸的實際繪值範圍。

set( gca, 'YTick', [-3:1:2] );

title( 'The North Hemisphere' );

plot( 1:12, bc, '-r.', 'MarkerSize', 10 );   子圖1中第1條曲線用實線繪,帶有圓點,紅色。MarkerSize屬性設 置圓點的大小是10。這樣畫出來的就是實心圓了。

plot( 1:12, nit, '-b.', 'MarkerSize', 10 );

plot( 1:12, sul, '-g.', 'MarkerSize', 10 );

plot( 1:12, poa, '-m.', 'MarkerSize', 10 );

plot( 1:12, soa, '-k.', 'MarkerSize', 10 );

%zeroArr = zeros( 14 );

%plot( 0:13, zeroArr, '--k' )

xlabel( 'Month' );

ylabel( 'Radiative Effect (Wm^-^2)' );  單位裏有上標,^表示後續一個字符爲上標。


下述代碼繪子圖二、三、4,雷同。

%--------------------------------------------------------------------------

% NH Radiative Forcing Fut-Mod  子圖2

fid_bc=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_NH\bc.dat','r');

bc = fscanf( fid_bc, '%f', [1,12]);

fclose( fid_bc );

fid_nit=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_NH\nit.dat','r');

nit = fscanf( fid_nit, '%f', [1,12]);

fclose( fid_nit );

fid_sul=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_NH\sul.dat','r');

sul = fscanf( fid_sul, '%f', [1,12]);

fclose( fid_sul );

fid_poa=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_NH\poa.dat','r');

poa = fscanf( fid_poa, '%f', [1,12]);

fclose( fid_poa );

fid_soa=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_NH\soa.dat','r');

soa = fscanf( fid_soa, '%f', [1,12]);

fclose( fid_soa );


%----------------------------------

h = subplot( 2, 2, 3, 'replace' );

po = get( h, 'Position' );

subplot( 2, 2, 3, 'replace' );

subplot( 'Position', [po(1)+0.03, po(2)+0.03, po(3), po(4)]);

%----------------------------------

box on;

hold on;

axis([0 13 -3 2]);

set( gca, 'XTick', [1:12] );

set( gca, 'YTick', [-3:1:2] );

%title( 'NH Fut-Mod' );

plot( 1:12, bc, '-r.', 'MarkerSize', 10 );

plot( 1:12, nit, '-b.', 'MarkerSize', 10 );

plot( 1:12, sul, '-g.', 'MarkerSize', 10 );

plot( 1:12, poa, '-m.', 'MarkerSize', 10 );

plot( 1:12, soa, '-k.', 'MarkerSize', 10 );

%zeroArr = zeros( 14 );

%plot( 0:13, zeroArr, '--k' )

xlabel( 'Month' );

ylabel( 'Radiative Forcing (Wm^-^2)' );


%--------------------------------------------------------------------------

% SH Radiative Effect Mod-Noall  子圖3

fid_bc=fopen('D:\_CurrentPaper\RadiativeForcing\ModNoall_SH\bc.dat','r');

bc = fscanf( fid_bc, '%f', [1,12]);

fclose( fid_bc );

fid_nit=fopen('D:\_CurrentPaper\RadiativeForcing\ModNoall_SH\nit.dat','r');

nit = fscanf( fid_nit, '%f', [1,12]);

fclose( fid_nit );

fid_sul=fopen('D:\_CurrentPaper\RadiativeForcing\ModNoall_SH\sul.dat','r');

sul = fscanf( fid_sul, '%f', [1,12]);

fclose( fid_sul );

fid_poa=fopen('D:\_CurrentPaper\RadiativeForcing\ModNoall_SH\poa.dat','r');

poa = fscanf( fid_poa, '%f', [1,12]);

fclose( fid_poa );

fid_soa=fopen('D:\_CurrentPaper\RadiativeForcing\ModNoall_SH\soa.dat','r');

soa = fscanf( fid_soa, '%f', [1,12]);

fclose( fid_soa );

%----------------------------------

h = subplot( 2, 2, 2, 'replace' );

po = get( h, 'Position' );

subplot( 2, 2, 2, 'replace' );

subplot( 'Position', [po(1)-0.03, po(2)-0.03, po(3), po(4)]);

%----------------------------------

box on;

hold on;

axis([0 13 -1.2 0.8]);

set( gca, 'XTick', [1:12] );

set( gca, 'YTick', [-1.2:0.4:0.8] );

title( 'The South Hemisphere' );

plot( 1:12, bc, '-r.', 'MarkerSize', 10 );

plot( 1:12, nit, '-b.', 'MarkerSize', 10 );

plot( 1:12, sul, '-g.', 'MarkerSize', 10 );

plot( 1:12, poa, '-m.', 'MarkerSize', 10 );

plot( 1:12, soa, '-k.', 'MarkerSize', 10 );

%zeroArr = zeros( 14 );

%plot( 0:13, zeroArr, '--k' )

xlabel( 'Month' );

%ylabel( 'Radiative Effect (Wm^-^2)' );


%--------------------------------------------------------------------------

% SH Radiative Forcing Fut-Mod  子圖4

fid_bc=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_SH\bc.dat','r');

bc = fscanf( fid_bc, '%f', [1,12]);

fclose( fid_bc );

fid_nit=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_SH\nit.dat','r');

nit = fscanf( fid_nit, '%f', [1,12]);

fclose( fid_nit );

fid_sul=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_SH\sul.dat','r');

sul = fscanf( fid_sul, '%f', [1,12]);

fclose( fid_sul );

fid_poa=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_SH\poa.dat','r');

poa = fscanf( fid_poa, '%f', [1,12]);

fclose( fid_poa );

fid_soa=fopen('D:\_CurrentPaper\RadiativeForcing\FutMod_SH\soa.dat','r');

soa = fscanf( fid_soa, '%f', [1,12]);

fclose( fid_soa );

%----------------------------------

h = subplot( 2, 2, 4, 'replace' );

po = get( h, 'Position' );

subplot( 2, 2, 4, 'replace' );

subplot( 'Position', [po(1)-0.03, po(2)+0.03, po(3), po(4)]);

%----------------------------------

box on;

hold on;

axis([0 13 -1.2 0.8]);

set( gca, 'XTick', [1:12] );

set( gca, 'YTick', [-1.2:0.4:0.8] );

%title( 'SH Fut-Mod' );

plot( 1:12, bc, '-r.', 'MarkerSize', 10 );

plot( 1:12, nit, '-b.', 'MarkerSize', 10 );

plot( 1:12, sul, '-g.', 'MarkerSize', 10 );

plot( 1:12, poa, '-m.', 'MarkerSize', 10 );

plot( 1:12, soa, '-k.', 'MarkerSize', 10 );

%zeroArr = zeros( 14 );

%plot( 0:13, zeroArr, '--k' )

xlabel( 'Month' );

%ylabel( 'Radiative Forcing (Wm^-^2)' );


我將legend放在了子圖4上。

gca=legend( 'BC', 'Nitrate', 'Sulfate', 'POA', 'SOA', 4 );  4表示把legend放在子圖的右下角,還有幾個數字的含義是:

       0 = Automatic "best" placement (least conflict with data)

      1 = Upper right-hand corner (default)

      2 = Upper left-hand corner

      3 = Lower left-hand corner

      4 = Lower right-hand corner

     -1 = To the right of the plot


po=get( gca, 'Position' ); 發現這樣放置後legend要擋住圖,所以須要再微調一下。得到legend的'Position'值。

set( gca, 'FontSize', 8, 'Position', [po(1)-0.01, po(2)+0.01, po(3), po(4)] ); 從新設置legend的位置,同時設置legend裏面的字體爲8號。

legend('boxoff');  不畫legend的外框。


強調的是上述調整legend的值要不斷地試。由於legend相對子圖的位置還要隨畫圖窗口大小變 化而變化。若是你看不懂這句,試試就知道了。


我通常是將MATLAB畫出的圖打印成PDF,再用Acrobat打開截屏,貼到WORD中,這樣圖 像質量彷佛比較好。誰還有更好的將MATLAB圖轉貼到WORD的方法,歡迎賜教。

相關文章
相關標籤/搜索