【问题标题】:How to have a common legend for subplots?如何为子图有一个共同的图例?
【发布时间】:2017-05-18 04:11:35
【问题描述】:

我正在尝试创建一个子图。我不希望子图有传说,而是要让人物有一个整体的传说。

我读到,可以通过仅向最后一个子图添加图例并通过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

【问题讨论】:

    标签: matlab matlab-figure legend figure subplot


    【解决方案1】:

    代码:

    % 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
    

    输出:

    【讨论】:

    • 我的收获是你可以在子图中增加0.5,不知道!整洁的解决方案。
    • 我要补充一点,subplot() 的第一个参数必须是整数,但其他参数显然不必是!
    猜你喜欢
    • 2019-12-23
    • 2014-12-29
    • 2018-09-24
    • 2013-05-20
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多