Matlab。将图例放置在情节之外

Matlab。将图例放置在情节之外,matlab,plot,legend,Matlab,Plot,Legend,我有以下代码 T = [0:1:30] a = [5:1:35]; a2 = [0:1:30]; b = [-4:1:26]; b2 = [12:1:42]; c = [16:1:46]; c2 = [15:1:45]; d = [2:1:32]; d2 = [-5:1:25]; figure(1) title('Time histories of output variables (measured vs estimated)') subplot(411),plot(T,a, T,a2,'r'

我有以下代码

T = [0:1:30]
a = [5:1:35]; a2 = [0:1:30];
b = [-4:1:26]; b2 = [12:1:42];
c = [16:1:46]; c2 = [15:1:45];
d = [2:1:32]; d2 = [-5:1:25];
figure(1)
title('Time histories of output variables (measured vs estimated)')
subplot(411),plot(T,a, T,a2,'r'); grid; ylabel('p (°/s)'); 
subplot(412),plot(T,b, T,b2,'r'); grid; ylabel('r (°/s)');
subplot(413),plot(T,c, T,c2,'r'); grid; ylabel('phi (º)');
subplot(414),plot(T,d, T,d2,'r'); grid; ylabel('ay (m/s2)');
legend('measured','estimated','Location','bestoutside')
xlabel('Time [s]');  
这将生成以下绘图


我想有一个故事以外的情节,他们所有的正常大小保持不变。(因此图例应该在其中一个红色圆圈中。有解决方案吗?

尝试使用
get
方法获取手柄位置,并通过执行一些算术手动更改位置。例如

T = [0:1:30];
a = [5:1:35]; a2 = [0:1:30];
b = [-4:1:26]; b2 = [12:1:42];
c = [16:1:46]; c2 = [15:1:45];
d = [2:1:32]; d2 = [-5:1:25];
figure(1)

title('Time histories of output variables (measured vs estimated)')

f1 = subplot(411);plot(T,a, T,a2,'r'); grid; ylabel('p (°/s)'); 
pos_f1 = get(f1,'Position');
hl = legend('measured','estimated','Location','bestoutside');
pos_hl = get(hl, 'Position');

subplot(412),plot(T,b, T,b2,'r'); grid; ylabel('r (°/s)');
subplot(413),plot(T,c, T,c2,'r'); grid; ylabel('phi (º)');
subplot(414),plot(T,d, T,d2,'r'); grid; ylabel('ay (m/s2)');

set(hl,'Position',[pos_f1(1)+pos_f1(3)-pos_hl(3)...
pos_hl(2)+pos_hl(4)+0.015...
pos_hl(3)...
0.5*pos_hl(4)]);
这应该给你:

您可能需要使用最后一行中的参数,但您已经有了主意。

您可以添加另一个作为空区域以仅容纳,轴可见性关闭,并且打印线的值为
'YData'
,因此它们不会渲染:

figure(1);
hSub = subplot(511); plot(1, nan, 1, nan, 'r'); set(hSub, 'Visible', 'off');
subplot(512); plot(T, a, T, a2, 'r'); grid; ylabel('p (°/s)');
subplot(513); plot(T, b, T, b2, 'r'); grid; ylabel('r (°/s)');
subplot(514); plot(T, c, T, c2, 'r'); grid; ylabel('phi (º)');
subplot(515); plot(T, d, T, d2, 'r'); grid; ylabel('ay (m/s2)');
xlabel('Time [s]');
legend(hSub, 'measured', 'estimated', 'Location', 'east');
结果如下:


我按照您的建议运行了代码,空的子批次在那里,但图例没有显示。我收到错误“警告:绘图为空。>在图例286中@FernandoBastosGarcía:您运行的是什么版本的MATLAB?2013B。总之,我找到了一个解决方案,绘图(1,1,1,1,'r')而不是nans@FernandoBastosGarcía:我在R2013a中测试了它,它似乎不喜欢
'XData'
'YData'
都设置为
nan
。只需将
'YData'
设置为
nan
(以及
'XData'
设置为任何值)修复它,使其适用于较新和较旧版本。此方法很有希望,但可能有点粗略。如果将其移到图形外部,则在将图形保存到ppt时,图例将不会显示。