Click items in the legend to show or hide the associated line in plot

121 ビュー (過去 30 日間)
Abdullah Kaya
Abdullah Kaya 2023 年 1 月 31 日
Hello everybody,
To start with an example will be easier for me, so lets assume that
I have 3 files all of them include some x values and some y values.
x axis is for frequency (i.e 8 to 10 GHz)
y axis is for epsilon corresponding to its frequency
I want to compare epsilon corresponding to its frequency of these 3 files by plotting the data. Files are formatted as *.csv.
Basically I use plot function with hold on command.
figure
plot(freq,eps1);
hold on;
plot(freq,eps2);
plot(freq,eps3);
xlabel("Frequency [GHz]"); ylabel("\epsilon")
legend("eps1","eps2","eps3")
Which gives me normal plot. What I want to do is that when you click the "eps2" on the legend for example, hide the line from the graph and only compare eps1 and eps3. And also to show the hided line, click to its legend name again
I use rfplot with VNA data's and that plot type has this kind of feature for sparameters plot.
Can we use this feature for any plot function?
  2 件のコメント
dpb
dpb 2023 年 1 月 31 日
There's example code in the <doc for legend callback> that illustrates how to do this.
Abdullah Kaya
Abdullah Kaya 2023 年 2 月 1 日
I did read about legend features but I believe I missed it. Thanks :)

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

採用された回答

Voss
Voss 2023 年 1 月 31 日
freq = 1:10;
eps1 = rand(10,1);
eps2 = rand(10,1);
eps3 = rand(10,1);
figure
plot(freq,eps1);
hold on;
plot(freq,eps2);
plot(freq,eps3);
xlabel("Frequency [GHz]"); ylabel("\epsilon")
legend("eps1","eps2","eps3",'ItemHitFcn',@cb_legend)
function cb_legend(~,evt)
if strcmp(evt.Peer.Visible,'on')
evt.Peer.Visible = 'off';
else
evt.Peer.Visible = 'on';
end
end
  4 件のコメント
Christoph Karbacher
Christoph Karbacher 2023 年 4 月 5 日
Thanks a lot.
That is pretty easy and cool. Is there a setting to hide all lines exept mayby the first one by default?
Kind regards Christoph
Christoph Karbacher
Christoph Karbacher 2023 年 4 月 5 日
Ok, got it.. You have to set the visibilty of the plot to "off"
like
plot(freq,eps2,'Visible','off');

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by