How can I use a plot legend to toggle an element's visibility? (2024)

40 views (last 30 days)

Show older comments

alessandro marino on 23 Apr 2023

  • Link

    Direct link to this question

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility

  • Link

    Direct link to this question

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility

Commented: alessandro marino on 1 May 2023

Accepted Answer: Aditya Srikar

Hi,

I am trying to plot multiple items on the same plot, but I would like to implement a quick method to hide or show a single item, e.g. by clicking on it on the plot legend. I have tried with the clickable legend (as described here: https://www.mathworks.com/matlabcentral/fileexchange/21799-clickablelegend-interactive-highlighting-of-data-in-figures), but this functionality seems to be unsupported in more recent MATLAB releases. I have also tried to include a callback function (as in this example: https://blogs.mathworks.com/pick/2016/03/25/interactive-legend-in-r2016a/), again with no results.

Thanks in advance for your help

7 Comments

Show 5 older commentsHide 5 older comments

dpb on 23 Apr 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2717363

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2717363

Have you tried <the example in the doc> of precisely that functionality?

alessandro marino on 24 Apr 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2718348

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2718348

I have, with no successful results

dpb on 24 Apr 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2718698

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2718698

Show us a SMALL working example that illustrates the issue; while I haven't done it, it's hard to imagine the doc example doesn't work...which specific release are you using?

dpb on 24 Apr 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2718718

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2718718

Edited: dpb on 24 Apr 2023

I dunno if can do this interactively here or not...

function hitcallback_ex1(src,evnt)

if strcmp(evnt.Peer.Visible,'on')

evnt.Peer.Visible = 'off';

else

evnt.Peer.Visible = 'on';

end

end

plot(rand(4));

l = legend('Line 1','Line 2','Line 3','Line 4');

l.ItemHitFcn = @hitcallback_ex1;

No, can't do that here, apparently; there's a whole lot about using this interface i've yet to learn/understand what can/cannot do...

Anyways, putting the above function into an m-file and executing the example code works as advertised here on R2020b...I presume it'll work on more recent releases as well...can toggle each and every line in the plot from the legend label associated...

alessandro marino on 24 Apr 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2718768

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2718768

Hi @dpb,

Thanks a lot, I tried to copy your code and it works! it looks like simpe stuff, but it was my first time studying MATLAB functions, so thanks a lot for your help.

I noticed that, when hiding one function, the y-axis adjusts to maximise the window on the remaining functions. I added the ylim command this way:

plot(rand(4));

ylim("manual");

l = legend('Line 1','Line 2','Line 3','Line 4');

l.ItemHitFcn = @hitcallback_ex1;

Not sure if ther is a more elegant way of doing it, but it works so far.

Thanks a lot again

Davindra Usov on 25 Apr 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2719469

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2719469

have you tried switch cases with a tiled layout. Search switch on matlab help

dpb on 25 Apr 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2719499

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2719499

Edited: dpb on 25 Apr 2023

@Davindra Usov -- what's the issue you're raising? I don't follow; I just created a tiled layout and put a legend on each of two axes/tiles and assigned the above callback function to each. Both work as advertised with no need to do anything else...

% from example for tiledlayout...

x = linspace(0,30);

y1 = sin(x/2);

y2 = sin(x/3);

y3 = sin(x/4);

% Plot into first tile twice... (doc does three, but two's plenty to illustrate)

tiledlayout('flow')

nexttile

plot(x,y1)

hLg(1)=legend('Y1'); % add a legend; save the handle...

nexttile

plot(x,y2)

hLg(2)=legend('Y2'); % add a legend; save the handle...

hLg(1).ItemHitFcn = @hitcallback_ex1;

hLg(2).ItemHitFcn = @hitcallback_ex1;

After this, clicking on the legend entry in each toggles visibility of each as expected...

set(hLg,{'ItemHitFcn'},{@hitcallback_ex1})

is a more elegant syntax to set all handles to the one callback function.

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Aditya Srikar on 28 Apr 2023

  • Link

    Direct link to this answer

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#answer_1225269

  • Link

    Direct link to this answer

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#answer_1225269

Edited: Aditya Srikar on 28 Apr 2023

Hi Alessandro

You can modify legend properties to control the appearance and behaviour of a Legend object. By changing each property, you can modify certain aspects of the legend.

Consider the following example

plot(rand(3))

lgd = legend('a','b','c');

c = lgd.TextColor;

lgd.TextColor = 'red';

In the above example, the text color of legend is set to red.

To toggle visibility of legend on click event, you can use the property ItemHitFcn, which associates a callback function to legend and the callback function executes when you click legend items.

Refer the documentation for more details :

https://in.mathworks.com/help/matlab/ref/matlab.graphics.illustration.legend-properties.html

Hope it helps !

1 Comment

Show -1 older commentsHide -1 older comments

alessandro marino on 1 May 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2727824

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1951673-how-can-i-use-a-plot-legend-to-toggle-an-element-s-visibility#comment_2727824

Thank you @Aditya Srikar, this helped, together with this comment by @dpb

Thank you all

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend

Find more on Legend in Help Center and File Exchange

Tags

  • plot
  • legend
  • interactive legend

Products

  • MATLAB

Release

R2021b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How can I use a plot legend to toggle an element's visibility? (11)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How can I use a plot legend to toggle an element's visibility? (2024)
Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5775

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.