View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
clickableLegend - Interactive highlighting of data in figures

5.0 | 27 ratings Rate this file 42 Downloads (last 30 days) File Size: 213 KB File ID: #21799 Version:
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 (39)
03 Feb 2017 ASML

ASML (view profile)

12 Jan 2017 Sugar Rush

15 Nov 2016 goc3

goc3 (view profile)

27 Oct 2016 Alton Vaughan

05 Oct 2016 William Cable

I was having the same problem as Royi Avital with the color being outside the [0,1] range. To get it to work with 2016a I modified the togglevisibility function within clickableLegend to the following:

function togglevisibility(hObject, obj)
if get(hObject, 'UserData') % It is on, turn it off
set(hObject, 'Color', [0.5 0.5 0.5],'FontAngle','italic', 'UserData', false);
set(obj, 'HitTest','on','visible','on','handlevisibility','on');
set(hObject, 'Color', [0 0 0],'FontAngle','normal', 'UserData', true);

01 Aug 2016 Stefan Engbers

I also have the problem Royi Avital has. Is there a solution yet?

Comment only
28 Jun 2016 Renwen Lin

Same Problem´╝Ü I have some issues with MATLAB R2016a. The color is outside the [0, 1] range.

at clickableLegend>togglevisibility (line 139)
set(hObject, 'Color', get(hObject, 'Color')*1.5 - 1, 'UserData', true);

12 Apr 2016 Royi Avital

I have some issues with MATLAB R2016a. The color is outside the [0, 1] range.

Comment only
28 Mar 2016 Josef Laumer

Great submission! Found the same issue like Alexander Stepanov, not every object has the MarkerSize poperty. Something like:
"if isprop(plotObj(i),'MarkerSize')
set(plotObj(i), 'LineWidth', lw+2, 'MarkerSize', get(plotObj(i),'MarkerSize')+2);
set(plotObj(i), 'LineWidth', lw+2, 'FontWeight', 'bold');
is needed. Would be great if the script could be updated.

21 Dec 2015 Alexander Stepanov

One small issue - selection does not work with scatter plots, as they do not have a MarkerSize property. The code should either check if the property exists before trying to get/set it, or use a pre-defined set of properties based on graphics object type. Some of the types have changed with HG2 - e.g. 'hggroup' seems to be replaced with 'scatter', so probably the former is easier).

Comment only
21 Dec 2015 Alexander Stepanov

And a small comment on setting data tip position in my code (lines 197-198). In 2015b the code works, but data tip for some reason is not shown where you expect it to be. The following code works, but it needs slight tweaks to be backwards compatible:

pt = get(axh, 'CurrentPoint'); % Get the location of the click
X = get(clickedPlotObj,'Xdata'); % Retrieve Xdata
Y = get(clickedPlotObj,'Ydata'); % Retrieve Ydata
axisUnits = get(axh, 'DataAspectRatio'); % Relative lenghts of data units along each axis
dist = ((X-pt(1,1))/axisUnits(1)).^2 + ((Y-pt(1,2))/axisUnits(2)).^2; % Distances to clicked point on (X,Y) plane
idx = find(dist == min(dist),1); % Index the click
cursor = get(hTag, 'Cursor'); % Specific to recent Matlab releases
cursor.DataIndex = idx;

Comment only
18 Dec 2015 Alexander Stepanov

One small issue I found with my code (link below) - apparently @ancestor doesn't return a cell even if you wrap the argument in a cell array, unlike @get function, so lines 115-116 need to be changed to:

parentAxes = ancestor(plothan, 'Axes');
if iscell(parentAxes)
parentAxes = [parentAxes{:}];

Comment only
17 Dec 2015 Alexander Stepanov

Thanks. Btw, I have modified the code to achieve everything I mentioned, specifically:
- Clicking on a plot shows a data tip. Clicking on it again simply moves the data tip, keeping line(s) selected.
- Clicking on empty space inside axes removes all selections.
- Moving selected items on top of UI stack (& reverting back once deselected)

If you have time, could you please take a look and consider updating your submission, so everyone could benefit from these features?

I tried to keep as much of the original code intact as possible, and I did not update any of the documentation. Tested in 2015b.

Comment only
17 Dec 2015 Adrian Cherry

Adrian Cherry (view profile)

  • 1 file
  • 4.20833

Alexander - with respect to your "Finally" bit. You can use gridLegend and clickableLegend together and I do. The function gridLegend tests to see if you have clickableLegend installed, if you do then it will use it.

Comment only
16 Dec 2015 Alexander Stepanov

Very nice. Great that it is now possible to highlight a line by clicking on it.

Some ideas if you plan to improve it further. There are two features it still lacks to completely remove the need to use @interactivelegend (another FEX utility) alongside this one:
1. Deselecting all lines when clicking on an empty space inside axes (much more convenient than having to click again on each line).
2. Displaying a label (same as in the legend) next to the the point you clicked on. When there are too many lines, it doesn't make sense to display the legend at all, so simply highlighting a line doesn't tell you which one it is. Displaying a label when you select a line solves the problem. That's exactly what @interactivelegend does.

Finally, would be nice to have control over which legend function is used as a base - e.g. so we could use @gridLegend utility from FEX, together with clickableLegend.

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

01 Sep 2016

Updated license

Contact us