matlab - 如何为子图提供共同的图例?

标签 matlab matlab-figure legend figure subplot

我正在尝试创建一个子图图。我不希望子图有图例,而是图有整体图例。

我读到可以通过仅向最后一个子图添加图例并通过 legendposition 功能调整其在图中的位置,或者使用一个子图图形位置(例如 subplot(2,3,5.5) 仅用于显示图例)。我更喜欢第二种选择,尽管直到现在我还没有成功。有什么帮助吗?

这是我的代码:

SLS=figure();
hold on
subplot(3,2,1);
plot(t,u{1},t,u{2},t,u{3},t,u{4},t,u{5},t,u{6});
title('SLS Levels');
subplot(3,2,2);
plot(t,log_u{1},t,log_u{2},t,log_u{3},t,log_u{4},t,log_u{5},t,log_u{6});
title('SLS Logarithms');
subplot(3,2,3);
plot(t,I_u{1},t,I_u{2},t,I_u{3},t,I_u{4},t,I_u{5},t,I_u{6});
title('SLS Levels with Intercept');
subplot(3,2,4);
plot(t,log_I_u{1},t,log_I_u{2},t,log_I_u{3},t,log_I_u{4},t,log_I_u{5},t,log_I_u{6});
title('SLS Log. with Intercept');
subplot(3,2,5.5);
legend('GDP', 'C', 'I', 'G', 'Imp.', 'Exp.');
axis off

最佳答案

代码:

% Plotting some random data and storing their handles
subplot(3,2,1);       h1 = plot(randperm(10),randperm(10),'ko-');
subplot(3,2,2);       h2 = plot(randperm(10),randperm(10),'g+-');
subplot(3,2,3);       h3 = plot(randperm(10),randperm(10),'md-');
subplot(3,2,4);       h4 = plot(randperm(10),randperm(10),'rv-.');

hL = subplot(3,2,5.5);
poshL = get(hL,'position');     % Getting its position

lgd = legend(hL,[h1;h2;h3;h4],'RandomPlot1','RandomPlot2','RandomPlot3','RandomPlot4');
set(lgd,'position',poshL);      % Adjusting legend's position
axis(hL,'off');                 % Turning its axis off

输出:

enter image description here

关于matlab - 如何为子图提供共同的图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41454174/

相关文章:

matlab - 使用散点图可视化大型 3D 数据集

图上的 MATLAB 简单 slider

python - 除了 Seafile 热图之外还带有标签的图例

MATLAB的graycomatrix和graycoprops的Python实现

matlab - 在 Matlab 中调整图例间距

matlab - 如何在matlab中添加条形图的三个图例?

python - matplotlib:颜色条及其文本标签

R 版的 scatter3(matlab)

matlab - 如何在 MATLAB 中控制双矩阵的显示?

excel - 如何使用 OpenOffice 通过 Matlab 写入 Excel 文件?