Plot both global and local legend

9 ビュー (過去 30 日間)
Mike Buba
Mike Buba 2024 年 1 月 27 日
コメント済み: Mike Buba 2024 年 2 月 5 日
In the tiledlayout, I am trying to plot both global and local legend.
X = rand(5);
Y = rand(5);
figure
tiledlayout(2,1);
nexttile
plot(X(1,:),Y(1,:), 'r'); hold on;
plot(X(2,:),Y(2,:), 'k'); hold on;
plot(X(3,:),Y(3,:), 'k--'); hold on;
legend('Reference', 'Location', 'NorthWest');
nexttile
plot(X(1,:),Y(1,:), 'r', 'HandleVisibility','off'); hold on;
plot(X(4,:),Y(4,:), 'k'); hold on;
plot(X(5,:),Y(5,:), 'k--'); hold on;
% legend('Reference', 'Location', 'NorthWest');
My_LGD = legend('Signal 1', 'Signal 2');
My_LGD.Orientation = "horizontal";
My_LGD.NumColumns = 4;
My_LGD.Layout.Tile = "South";
This is the minimal working example I am using. I want to have a global legend showing Signal 1 and Signal 2 and then a local legend to highlight the e.g., Reference signal.
I had to hide the first plot in the second plot 'HandleVisibility','off' so my global legend takes the second two signals. Otherwise, is shows red line next to Signal 1 and black solid line next to Signal 2.
This is what I get What I want
You can see in the second Figure, that local legend is in both plots (what I want), and in the first figure is what the code gives me.
  1 件のコメント
Mike Buba
Mike Buba 2024 年 2 月 4 日
編集済み: Mike Buba 2024 年 2 月 4 日
If I slightly modify the code; add ax=axes('Position',get(gca,'Position'),'Visible','Off'); and assigned p1, p2, etc. to the each plot. the plotting the last legend is done as: My_LGD = legend(ax, [p2 p3], {'Signal 1', 'Signal 2'});
X = rand(5);
Y = rand(5);
figure
tiledlayout(2,1);
nexttile
p1 = plot(X(1,:),Y(1,:), 'r'); hold on;
p2 = plot(X(2,:),Y(2,:), 'k'); hold on;
p3 = plot(X(3,:),Y(3,:), 'k--'); hold on;
legend('Reference', 'Location', 'NorthWest');
nexttile
p1 = plot(X(1,:),Y(1,:), 'r'); hold on;
p4 = plot(X(4,:),Y(4,:), 'k'); hold on;
p6 = plot(X(5,:),Y(5,:), 'k--'); hold on;
legend('Reference', 'Location', 'NorthWest');
ax=axes('Position',get(gca,'Position'),'Visible','Off');
My_LGD = legend(ax, [p2 p3], {'Signal 1', 'Signal 2'});
My_LGD.Orientation = "horizontal";
My_LGD.NumColumns = 4;
My_LGD.Layout.Tile = "South";
However, the My_LGD.Layout.Tile = "South"; gives an error:
Property assignment is not allowed when the object is empty.
Use subscripted assignment to create an array element.
Error in test (line 20)
My_LGD.Layout.Tile = "South";
It seems I can't get a global legend to appear outside the plot. Anyone knows what to do?

サインインしてコメントする。

採用された回答

Benjamin Kraus
Benjamin Kraus 2024 年 2 月 4 日
編集済み: Benjamin Kraus 2024 年 2 月 4 日
Your hidden axes (and global legend) needs to have the tiledlayout as a parent.
X = rand(5);
Y = rand(5);
figure
tcl = tiledlayout(2,1); % I added an output argument from tiledlayout here.
nexttile
p1 = plot(X(1,:),Y(1,:), 'r'); hold on;
p2 = plot(X(2,:),Y(2,:), 'k'); hold on;
p3 = plot(X(3,:),Y(3,:), 'k--'); hold on;
legend('Reference', 'Location', 'NorthWest');
nexttile
p1 = plot(X(1,:),Y(1,:), 'r'); hold on;
p4 = plot(X(4,:),Y(4,:), 'k'); hold on;
p6 = plot(X(5,:),Y(5,:), 'k--'); hold on;
legend('Reference', 'Location', 'NorthWest');
ax=axes(tcl,'Visible','Off'); % I modified this line to specify the parent and ignore position.
My_LGD = legend(ax, [p2 p3], {'Signal 1', 'Signal 2'});
My_LGD.Orientation = "horizontal";
My_LGD.NumColumns = 4;
My_LGD.Layout.Tile = "South";
  1 件のコメント
Mike Buba
Mike Buba 2024 年 2 月 5 日
Thanky you @Benjamin Kraus!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by