Code covered by the BSD License  

Highlights from
clickableLegend

5.0

5.0 | 14 ratings Rate this file 26 Downloads (last 30 days) File Size: 3.03 KB File ID: #21799
image thumbnail

clickableLegend

by Ameya Deoras

 

17 Oct 2008 (Updated 24 May 2009)

A legend with clickable strings that let you hide and show objects in a plot.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

clickableLegend is a wrapper around the LEGEND function that provides the added functionality to turn on and off (hide or show) a graphics object (line or patch) by clicking on its text label in the legend. Its usage is the same as the LEGEND function in MATLAB.

Notes:

1. You can group multiple lines into a single legend entry and toggle their visibility with a single click.

2. The DisplayedLines parameter lets you specify which lines should be displayed initially. This is useful if you have a large number of lines but want to start out with only a few shown.

3. If you save the figure and re-load it, the toggling functionality is not automatically re-enabled. To restore it, simply call clickableLegend with no arguments.

4. To prevent the axis from automatically scaling every time a line is turned on and off, issue the command: axis manual

Example 1:
z = peaks(100);
plot(z(:,26:5:50))
grid on;
axis manual;
clickableLegend({'Line1','Line2','Line3','Line4','Line5'}, 'Location', 'NorthWest');

Example 2:
f = plot([1:10;1:2:20]','x'); hold on;
g = plot(0:.1:10,sin([0:.1:10;0:.2:20]'),'r-');
h = plot(11:20,rand(5,10)*5,'b:');
clickableLegend([f;g;h], {'Line1','Line2','Line3'},...
  'groups', [1 1 2 2 3 3 3 3 3], 'displayedLines', [2 3]);

Acknowledgements
This submission has inspired the following:
gridLegend - a multi column format for legends
MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (15)
20 Oct 2008 Thierry Dalon

This is a feature I've requested as enhancement to TheMathworks for the base legend function.
Thank you for implementing it. I will give it a try.

21 Oct 2008 Thierry Dalon

Excellent. It does what it sells. Very well coded as well.
Maybe the name of the function is a bit long.... I've changed it to ilegend.
Thanks.

21 Oct 2008 Riccardo Meldolesi

Great functionality.

I learnt from the code too!

Well done

21 Oct 2008 Martin Richard

Very good indeed. Congratulations!

11 Nov 2008 Richard

Funny when I first saw this I thought: "nice but I don't really need it". Then today I was plotting a bunch of data and was wishing I had someone to "turn off" some of the lines. Just what I needed! Thanks for the great post.

04 Apr 2009 Hoi Wong

Great idea! You saved my butt for my project.

04 Apr 2009 Jveer

this is good stuff! how abt you get cracking on something similar for colorbar - i already have a name for it clickable colorbar ;)

26 Sep 2009 Jeff Preston  
25 Nov 2009 Lukas Lansky  
09 Nov 2010 Barry

Excellent!

27 May 2011 Evgeny Pr  
11 Jun 2011 Bernd

very nice and easy to handle tool...I tried it on a bunch of old plots and it works just fine...my pick of the month!

15 Jun 2011 Bernd Meister  
11 Aug 2011 Ben

Nice work!

01 Sep 2011 Vanich

Super awesome

Please login to add a comment or rating.
Updates
24 May 2009

Added ability to group lines into single legend entries and specify which lines are displayed initially.

Tag Activity for this File
Tag Applied By Date/Time
annotation Ameya Deoras 22 Oct 2008 10:25:07
customization Ameya Deoras 22 Oct 2008 10:25:07
hide Ameya Deoras 22 Oct 2008 10:25:07
show Ameya Deoras 22 Oct 2008 10:25:07
toggle Ameya Deoras 22 Oct 2008 10:25:07
visibility Ameya Deoras 22 Oct 2008 10:25:07
plot Ameya Deoras 22 Oct 2008 10:25:07
line Ameya Deoras 22 Oct 2008 10:25:07
patch Ameya Deoras 22 Oct 2008 10:25:07
legend Ameya Deoras 22 Oct 2008 10:25:07
figure Ameya Deoras 22 Oct 2008 10:25:07
enable Ameya Deoras 29 Oct 2008 15:02:11
click Ameya Deoras 29 Oct 2008 15:02:11
disable Ameya Deoras 29 Oct 2008 15:02:11
mouse Ameya Deoras 29 Oct 2008 15:02:11
potw Cristina McIntire 07 Nov 2008 13:01:11
legend Arnaud Thabot 24 Mar 2009 20:30:36
pick of the week Jiro Doke 11 Feb 2011 20:11:25
visibility Joerg 14 Sep 2011 08:46:58
toggle Joerg 14 Sep 2011 08:47:01
legend Alexander 26 Oct 2011 07:49:17

Contact us at files@mathworks.com