image thumbnail

gridLegend - a multi column format for legends

version (5.3 KB) by Adrian Cherry
Plot a multicolumn format legend.


Updated 20 Jan 2016

View License

On a plot with a lot of traces the standard legend will often scroll off the bottom or the side of the figure, this function is intended to overcome this by allowing the user to define a multi column format for the legend.
Usage :
legHdl = gridLegend(hdl,nCols,gKey,'parameter_name','parameter_value',...);
Inputs :
hdl - a vector of graphic handles of the plotted data
nCols - the number of columns in the legend. (defaults to 2 if not defined)
gKey - a cell array of strings to display which should match the number of graphic handles in hdl (optional)
'parameter_name','parameter_value' - any parameter name pairs applicable to the legend
Output :
legHdl - a vector of graphic handles for the legend generated.
Notes :
By default the legend will be arranged vertically, ie the second legend trace is placed under the first trace, filling the first column before moving onto the next. If you use the 'Orientation','Horizontal' parameter pair then the traces will be arranged horizontally, so the second trace will be in the second
column and so on to the end of the row, then moving down to the next row.
The parameter name / value pairs are passed over to the legend function so you can set things like 'Fontsize',8 etc.

The parameter / name pair of location can be used to place the legend - available options are
'location','bestoutside' : for 2 column legend uses 'eastoutside', more than 2 it uses 'southoutside'
'location','north' : places the legend centrally at the top of the axes
'location','northoutside' : places the legend centrally at the top of the figure
'location','eastoutside' : places the legend on the right of the figure
'location','southoutside' : places the legend centrally at the bottom of the figure
'location','westoutside' : places the legend on the left of the figure

By default this is set to 'bestoutside'

This will work on x-y plots, bar and errorbar charts.

With many traces it's sometimes difficult to work out which trace the legend is referring to. From the MATLAB file exchange I've used a function called 'clickableLegend' which allows you to switch the plotted data on and off by clicking on the Legend key. This function will use clickableLegend if it can find it in your MATLAB path, otherwise it will revert back to the standard legend function.

Examples :

This will plot a vertical legend with two columns

This will plot a horizontal legend, 8 traces per row, and fontsize set to 8 points.

This will plot a vertical legend on the left hand side with 2 columns with the box switched off.

Note with versions earlier than R1014b you have to fiddle with the XColor and YColor
otherwise we end up with black lines for the X and Y axis.
gridLegend(hdl,2,gKey,'location','westoutside','Fontsize',8,'Box','off','XColor',[1 1 1],'YColor',[1 1 1])

Cite As

Adrian Cherry (2022). gridLegend - a multi column format for legends (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!