subplot などで作成した複数の Axes に対して、一つの共通した legend( 凡例)を作成するにはどうすればよいですか?

53 visualizzazioni (ultimi 30 giorni)
MathWorks Support Team
MathWorks Support Team il 29 Lug 2020
subplot 関数を使って、Figure 上に複数の Axes を表示しています。
表示しているデータは、すべての Axes で共通した内容のため、legend (凡例)は、一つだけ表示させたいのですが、以下のように実行すると、一番下の Axes だけサイズが揃いません。
f = figure;
c = categorical({'January','July'});
subplot(3, 1, 1)
bar(c,rand(2, 2))
subplot(3, 1, 2)
bar(c,rand(2, 2))
subplot(3, 1, 3)
bh = bar(c,rand(2, 2));
% legend での表示名
bh(1).DisplayName = string(c(1));
bh(2).DisplayName = string(c(2));
legend('show','Location','southoutside') % 最下段の下に凡例を表示
tiledlayout や nexttile 関数で Axes を複数配置した場合も、legend の位置をバランスよく調整できません。

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 29 Lug 2020
現状、subplot 関数などで複数作成された Axes に対して、1つのlegend をバランスを考慮して作成する機能は MATLAB では提供されていません。
代替案として、マウスのドラッグ&ドロップで legend を Axesの領域から外側に移動するか、同様のことをプログラミングにて実装する方法が考えられます。
legend の表示位置は、Position プロパティにて調整可能です。
以下は、プログラミングにて実装した一例です。
f = figure;
c = categorical({'January','July'});
subplot(3, 1, 1)
bar(c,rand(2, 2))
subplot(3, 1, 2)
bar(c,rand(2, 2))
subplot(3, 1, 3)
bh = bar(c,rand(2, 2));
% legend での表示名
bh(1).DisplayName = string(c(1));
bh(2).DisplayName = string(c(2));
% Figure サイズの調整
f.Position(4) = f.Position(4) + 250;
movegui(f,'center') % 表示位置を調整
% 凡例の追加
Lgnd = legend('show');
Lgnd.Orientation = 'Horizontal';
Lgnd.Position(1) = 0.35;
Lgnd.Position(2) = 0.025;
Lgnd.Position(4) = 0.04;

Più risposte (0)

Prodotti


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!