5.0

5.0 | 7 ratings Rate this file 50 downloads (last 30 days) File Size: 3.03 KB File ID: #21799

clickableLegend

by Ameya Deoras

 

17 Oct 2008 (Updated 24 May 2009)

Code covered by BSD License  

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

Download Now | 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]);

MATLAB release MATLAB 7.6 (R2008a)
Zip File Content  
Other Files clickableLegend.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
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  
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
figure Ameya Deoras 22 Oct 2008 10:25:07
legend Ameya Deoras 22 Oct 2008 10:25:07
patch 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
mouse Ameya Deoras 29 Oct 2008 15:02:11
disable 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
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com