How to create a scroll bar for the legend

11 views (last 30 days)
Hello everyone,
I thought that this would be a common question however I can't find any documentation or outside information on how to generate a scroll bar for a legend. So I have a plot that has over 100 plot labels in the legend. However the figure when maximized only shows about 30 of the labels and if I adjust the font so that all of labels fit the labels become unreadable. I was wondering if there is an easy way to just keep that font size as is but add a scroll bar to the legend so that I can scroll through all of the labels. Thank you in advance!!
  2 Comments
Joseph Cheng
Joseph Cheng on 19 May 2015
Can you give an example of what your figure with 100 plotted lines looks like? I have trouble visualizing a legend that could be readable. With that many lines and how they are located in relation to each other, I would assume you wouldn't be able to distinguish between the unique line/marker styles and colors. If you show an example perhaps there is a better way?
Peter
Peter on 19 May 2015
Your correct it is difficult to distinguish between the bulk of the data plots however there are outliers that we would like to identify. I'm not allowed to provide an example. Essentially all I would like to do is generate a scroll bar along the legend.

Sign in to comment.

Accepted Answer

Joseph Cheng
Joseph Cheng on 19 May 2015
how are you not allowed to provide an example with dummy data and unlabeled axes? I ask for an example to check that you've created 100 unique line styles/colors/marker combinations such that a legend is indeed useful. Otherwise how are you to determine which label corresponds to the not unique style. An example even with dummy data showing what an outlier looks like can be determined within the script/function and marked as an outlier so you don't have to display all the other legend items just outliers.
To my knowledge the built in legend function will not do this for you. However what you can do if you really want scrollable legend is create your own with uitable() and go through and pull out for each entry the line color, displayname, linestyle, and marker and create a table with one column of linestyle+marker formatted with the correct color, and the second column with the displayname.

More Answers (3)

Jan
Jan on 19 May 2015
A legend should help to identify specific lines in a diagram. With 100 different lines a legend is not usable anymore and in consequence not useful.
There are several submissions in the FileExchange for multi-line legends. Search for "legend" there.
  1 Comment
Peter
Peter on 19 May 2015
So does this mean there's no easy solution to add a scroll bar to a legend?

Sign in to comment.


Chad Greene
Chad Greene on 19 May 2015
I started this as a comment, but I think it turned into an answer. That answer is, don't do it.
I'm having a hard time envisioning a time that a legend with a scroll bar could ever be effective information design. As a viewer of a graph, it seems absolutely dreadful to have to go back and forth between legend and graph, trying to link data to so many labels. The back-and-forth process is bad enough for a legend of just a few entries--direct labels are almost always better than legends--but you want to give the viewer the chore of not only linking data to correct legend entries, but also scrolling through to find the right data and label?
A legend with a scroll bar also strips each legend entry of its context among other legend entries. Without the ability to see all markers in the legend at once, will the viewer have confidence that they have found the correct marker color and shape, instead of a marker that looks very similar to the correct marker that is hidden somewhere down the scroll menu?
Do not put the onus on the viewer to find the information they need. Make it easy for them. Show the important information in context and whenever possible, label data directly.

Walter Roberson
Walter Roberson on 19 May 2015
A legend is an axes with various things drawn in it. You can manipulate it as you would manipulate an axes, but being careful because it already has a lot of properties defined that tie it to the original data. For example listeners set so that when the properties of a line or lineseries object are changed, the legend gets updated without a specific legend() call being needed.
You could use some of the ideas from scrollsubplot() File Exchange Contribution for controlling axes visibility with a slider.
One trick I just found is that the legend text() objects in the legend are defined with Units 'normalized', but the line() objects are always Data units. If you set() the axes YLim properties to adjust the portions of the legend that are visible vertically (vertical scroll) then the line objects would move but the text would not. However if you go in and set() the text object Units to be 'data' then they will move when you change YLim. I did find, though, that the box holding the figure appears to clip lines and markers but not text, so the text might be visible outside the legend; I don't have a solution for that at the moment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!