Matlab。将图例放在图外

标签 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'); 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

我希望图例位于情节之外,并且所有图例都保持正常大小。 (所以图例应该在那些红色圆圈之一中。有什么解决办法吗?

最佳答案

您可以添加另一个 subplot作为空区域仅容纳 legend , 轴可见性关闭并且 nan绘制线的 '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

关于Matlab。将图例放在图外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44349293/

相关文章:

r - 在 R 中实现 Matlab fspecial 函数

matlab - 将 Matlab 图导出为 PNG?

python - 在 Bokeh 图中隐藏图例

python - pyqtgraphplotwidget在错误区域绘制多个Y轴图

r - 如何将颜色和线型映射到 ggplot2 线图中的几列

matlab - .m 文件作为 MATLAB/Octave for Sublime Text

matlab - MATLAB 中神经网络分类的 10 倍交叉验证示例

matlab - 使用 opengl 硬件渲染器导出带有颜色条的 MATLAB 冲浪图会导致损坏的 png 文件

r - 将特定图例添加到 R 绘图中

r - 根据图中图例向量的长度更改图例中文本的大小