加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

【matlab】MATLAB中调整legend的大小位置

(2012-08-23 13:28:33)
标签:

杂谈

分类: MATLAB
http://hi.baidu.com/new/csudeng

MATLAB中plot命令绘图微调的几个注记

1、MATLAB如何从硬盘读取文件。

2、如何微调subplot子图的位置。

3、plot命令绘曲线时,曲线上的标志如何调整大小。

4、坐标轴的调整。

6、坐标标题中如何标上标。

7、如何调整图示(legend)的位置。

 

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

% 这里要画一个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)' );  单位里有上标,^表示后续一个字符为上标。

 

下述代码绘子图2、3、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的方法,欢迎赐教。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有