Code covered by the BSD License  

Highlights from

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



Ameya Deoras


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

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 Grid Legend A Multi Column Format For Legends and Fanchart Visualize Percentiles Of Time Series Data.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (22)
06 Jan 2015 Hoi Wong

Hoi Wong

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

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

Stephen Goldrick

06 Aug 2013 Amy


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


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


08 Aug 2012 Egon Geerardyn

Egon Geerardyn

01 Sep 2011 Vanich


Super awesome

11 Aug 2011 Ben


Nice work!

15 Jun 2011 Bernd Meister

Bernd Meister

11 Jun 2011 Bernd


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

09 Nov 2010 Barry



25 Nov 2009 Lukas Lansky

Lukas Lansky

26 Sep 2009 Jeff Preston

Jeff Preston

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

04 Apr 2009 Hoi Wong

Hoi Wong

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

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

Contact us