Code covered by the BSD License  

Highlights from
clickableLegend - Interactive highlighting of data in figures

5.0 | 19 ratings Rate this file 58 Downloads (last 30 days) File Size: 213 KB File ID: #21799 Version: 1.4
image thumbnail

clickableLegend - Interactive highlighting of data in figures


Ameya Deoras (view profile)


17 Oct 2008 (Updated )

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

**** UPDATE ****
ClickableLegend now also highlights entries when you click them in the figure/axes window. This function is also R2014b ready
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.


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


This file inspired Bubbleplot Multidimensional Scatter Plots, Grid Legend A Multi Column Format For Legends, and Fanchart Visualize Percentiles Of Time Series Data.

Required Products MATLAB
MATLAB release MATLAB 7.6 (R2008a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
02 Nov 2015 Andrey Vasilyev  
16 Aug 2015 Egon Geerardyn  
06 Jan 2015 Hoi Wong

Hoi Wong (view profile)

I just notice that if I disable a legend and the print (or do a print preview), the legend text will reset to black (instead of grey).

This will throw an error in this line "set(hObject, 'Color', get(hObject, 'Color')*1.5 - 1, 'UserData', true);" as the color is [0 0 0], [0 0 0]-1=[-1 -1 -1] (togglevisibility() lost track of the color state as it's externally modified by printing).

Any idea how to handle cases like this robustly? Right now I just put a floor function over it "max([0 0 0], get(hObject, 'Color')*1.5 - 1)" but it will take a few clicks to get it back to the right state.

Comment only
10 Dec 2014 Hoi Wong

Hoi Wong (view profile)

It seems like in R2014b, I cannot just override the already generated legend by just calling clickableLegend() or clickableLegend(gca). Anybody has the same issue?

In my case, the legends are generated by gscatter(), so I don't have control over it.

Comment only
16 Oct 2014 Stephen Goldrick  
06 Aug 2013 Amy

Amy (view profile)

Feature request: modify the code to work with any font color in the legend. When the legend font color is changed to a different color, for example blue ([0 0 1]), the function crashes when any legend entry is clicked. This is because the scaling of the font color results in a vector with values greater than 1.

Comment only
08 Apr 2013 Jim

Jim (view profile)

1. This is a very useful function when presenting a figure containing many data series to an audience.
2. Unfortunately there's an error in the 2nd example given in the preamble to the code. Instead of "'groups', [1 1 2 2 3 3 3 3 3]" in the clickableLegend call (which causes an error), I believe the intended usage was "'groups', [1 2 3 3 3 3 3]"
3. The behavior I wanted was to toggle data series visibility by clicking the associated legend text. The posted callback routine toggles the HitTest and HandleVisibility properties of the series, but to get the desired behavior, I replaced the two "set(obj, ...)" lines with




Comment only
19 Jan 2013 Amy

Amy (view profile)

08 Aug 2012 Egon Geerardyn  
01 Sep 2011 Vanich

Vanich (view profile)

Super awesome

11 Aug 2011 Ben

Ben (view profile)

Nice work!

15 Jun 2011 Bernd Meister  
11 Jun 2011 Bernd

Bernd (view profile)

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

27 May 2011 Evgeny Pr

Evgeny Pr (view profile)

09 Nov 2010 Barry

Barry (view profile)


25 Nov 2009 Lukas Lansky  
26 Sep 2009 Jeff Preston  
04 Apr 2009 Jveer

Jveer (view profile)

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

04 Apr 2009 Hoi Wong

Hoi Wong (view profile)

Great idea! You saved my butt for my project.

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.

21 Oct 2008 Martin Richard

Very good indeed. Congratulations!

21 Oct 2008 Riccardo Meldolesi

Great functionality.

I learnt from the code too!

Well done

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.

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.

Comment only
24 May 2009 1.2

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

15 Jul 2015 1.4

ClickableLegend now also highlights entries when you click them in the figure/axes window. This function is also R2014b ready

Contact us