gridLegend - a multi column format for legends

Plot a multicolumn format legend.
19,1K Descargas
Actualizado 20 ene 2016

Ver licencia

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
gridLegend(hdl,2)

This will plot a horizontal legend, 8 traces per row, and fontsize set to 8 points.
gridLegend(hdl,8,gKey,'Orientation','Horizontal','Fontsize',8)

This will plot a vertical legend on the left hand side with 2 columns with the box switched off.
gridLegend(hdl,2,gKey,'location','westoutside','Fontsize',8,'Box','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])

Citar como

Adrian Cherry (2024). gridLegend - a multi column format for legends (https://www.mathworks.com/matlabcentral/fileexchange/29248-gridlegend-a-multi-column-format-for-legends), MATLAB Central File Exchange. Recuperado .

Compatibilidad con la versión de MATLAB
Se creó con R2014b
Compatible con cualquier versión
Compatibilidad con las plataformas
Windows macOS Linux
Categorías
Más información sobre Legend en Help Center y MATLAB Answers.

Community Treasure Hunt

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

Start Hunting!
Versión Publicado Notas de la versión
1.4.0.0

Corrected North positioning

1.3.0.0

Should now work with subplots - demo updated to display this. Also corrected a problem of blank columns if a large number of columns requested and only a few lines per column.

1.2.0.0

The function should now be compatible with the new graphics system, i.e. R2014b or newer. I have tried to ensure it is still backward compatible with the earlier versions.

1.1.0.0

Updated to work with errorbar chart legends.

Also fixed an edge case where there is only one trace per column. It didn't like it if you had 5 traces and asked for 5 columns, it placed all the keys and labels in the first column.

1.0.0.0