【matlab】MATLAB中调整legend的大小位置
(2012-08-23 13:28:33)
标签:
杂谈 |
分类: MATLAB |
MATLAB中plot命令绘图微调的几个注记
1、MATLAB如何从硬盘读取文件。
2、如何微调subplot子图的位置。
3、plot命令绘曲线时,曲线上的标志如何调整大小。
4、坐标轴的调整。
6、坐标标题中如何标上标。
7、如何调整图示(legend)的位置。
%----------------------------------
%
这里要画一个2*2共4幅子图。先将第1个子图的位置调整。
h = subplot( 2, 2,
1);
po = get( h, 'Position'
);
subplot( 'Position', [po(1)+0.03, po(2)-0.03, po(3),
po(4)]);
%----------------------------------
hold on;
axis([0 13 -3 2]);
set( gca, 'XTick', [1:12]
);
set( gca, 'YTick', [-3:1:2] );
title( 'The North 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)'
);
下述代码绘子图2、3、4,雷同。
%--------------------------------------------------------------------------
% NH Radiative Forcing
Fut-Mod
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
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
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
);
po=get( gca, 'Position'
);
set( gca, 'FontSize', 8, 'Position', [po(1)-0.01, po(2)+0.01,
po(3), po(4)]
);
legend('boxoff');
强调的是上述调整legend的值要不断地试。因为legend相对子图的位置还要随画图窗口大小变 化而变化。如果你看不懂这句,试试就知道了。
我一般是将MATLAB画出的图打印成PDF,再用Acrobat打开截屏,贴到WORD中,这样图 像质量似乎比较好。谁还有更好的将MATLAB图转贴到WORD的方法,欢迎赐教。