Multiplot Legend Item remains after line visible property put to "off"

2 ビュー (過去 30 日間)
Jason
Jason 2024 年 6 月 18 日
編集済み: Ayush Modi 2024 年 6 月 18 日
Hi, on a previous post I was kindly showed how to "disable a plot" on a set of plots. What I actually want to do is temporarily remove it and Steven Lord suggested using the "visible" parameter
So I use a checkbox to allow me to toggle on or off the last line plotted:
val = app.VisibleOFFlastplotCheckBox.Value;
ax=app.UIAxes;
t = ax.Children(1); % Get last plot
switch val
case 1
t.Visible='off';
case 0
t.Visible='on';
end
Whilst this works, it doesn't seem to remove the legend item, but instead Greys it out.
So to show this, the purple curve below is the one I want to remove / hide (note if remove it, I want the possibility of adding it back which is why Steven suggested the "visible" approach)
And here's what happens once I set the visible property of the line to "off" - notice the legen item is still present
So how can I hide / temporarily remove the last legend item?
Thanks
Jason
  1 件のコメント
Aquatris
Aquatris 2024 年 6 月 18 日
By any chance does 't' has 'HandleVisibility' property? if so try to set that to off.

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

採用された回答

Jaynik
Jaynik 2024 年 6 月 18 日
Hi Jason,
Instead of just using the Visible property, you can set the IconDisplayStyle property of the Annotation object to 'off'. Here is how you can adjust the code accordingly:
switch val
case 0
t.Visible = 'off';
t.Annotation.LegendInformation.IconDisplayStyle = 'off';
case 1
t.Visible = 'on';
t.Annotation.LegendInformation.IconDisplayStyle = 'on';
end

その他の回答 (1 件)

Ayush Modi
Ayush Modi 2024 年 6 月 18 日
編集済み: Ayush Modi 2024 年 6 月 18 日
Hi Jason,
From the previous post, I understand you are using the 'DisplayName' property in the plot function and passing the plot handles in the legend function.
f1 = figure;
a = plot(2:10,3:11, 'DisplayName','legend1');
% Current legend statement - passing plot handles
legend(a);
a.HandleVisibility = "off";
a.Visible = 'off';
As depicted, 'HandleVisibility' property doesn't hide the legend line in this implementation. As a workaround, you can enable legends visibility using 'show' parameter. Here is the example code for your reference:
f2 = figure;
b = plot(2:10,3:11, 'DisplayName','legend2');
% Setting the legend property to show.
legend show;
b.HandleVisibility = "off";
b.Visible = 'off';
Hope this helps!

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by