web-dev-qa-db-ja.com

Matlab。凡例をプロットの外に配置する

私は次のコードを持っています

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]');  

これは次のプロットを生成します enter image description here

プロットの外に凡例を置き、それらはすべて通常のサイズのままにしておきたいと思います。 (したがって、凡例はそれらの赤い円の1つにあるはずです。解決策はありますか?

別の subplot を追加して、軸の可視性をオフにして nan で、 legend のみを保持する空の領域として機能させることができます。 =プロットされた線の'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');

そして、これが結果です:

enter image description here

7
gnovice

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)]);

これはあなたに与えるはずです: subplot with changed position

最後の行のパラメーターをいじくり回す必要があるかもしれませんが、アイデアは得られます。

1
kedarps