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

本文详细介绍了如何在MATLAB中通过plot命令微调图形,特别是调整legend的位置和大小。通过设置子图的位置、坐标轴属性、曲线标记及legend属性,实现了对图示(legend)的精确控制,包括改变其在右下角的位置以及调整字体大小,以避免遮挡图形。同时,分享了将MATLAB图形导入到Word中的经验技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源地址:

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的方法,欢迎赐教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值