13
$\begingroup$

I want to create a single column LineLegend with at least 12 different entries. Suppose that I define lists colors and names as follows:

colors = {Red, Magenta, Pink, Orange, Yellow, Green, Darker[Green], 
   Cyan, Blue, Purple, Black, Gray};
names = Table["Color " <> ToString[i], {i, 1, Length[colors]}];

If I use colors[[;; 10]] to show only the first 10 entries in LineLegend with LegendLayout -> "Column", I get a nice single column LineLegend:

LineLegend[colors[[;; 10]], names, LegendLayout -> "Column", 
 LabelStyle -> {20, FontFamily -> "Arial"}]

singleColumn

However, if I show 11 or more entries, the legends become double-columned:

LineLegend[colors[[;; 11]], names, LegendLayout -> "Column", 
 LabelStyle -> {20, FontFamily -> "Arial"}]

doubleColumn1

LineLegend[colors, names, LegendLayout -> "Column", 
 LabelStyle -> {20, FontFamily -> "Arial"}]

doubleColumn2

How can I force LineLegend to put the legend entries in a single column?

$\endgroup$
1
  • 2
    $\begingroup$ f[pairs_] := Grid[pairs]; LineLegend[colors, names, LegendLayout -> f, LabelStyle -> {20, FontFamily -> "Arial"}] gives !Mathematica graphics $\endgroup$
    – Nasser
    Commented Apr 12, 2014 at 17:09

2 Answers 2

7
$\begingroup$

You can define a layout function as shown in the documentation :-

colors = {Red, Magenta, Pink, Orange, Yellow, Green,
   Darker[Green], Cyan, Blue, Purple, Black, Gray};
names = Table["Color " <> ToString[i], {i, 1, Length[colors]}];

table[pairs_] := TableForm[pairs, TableAlignments -> Left,
   TableSpacing -> {2, 1}];

LineLegend[colors[[;; 11]], names, LegendLayout -> table, 
 LabelStyle -> {20, FontFamily -> "Arial"}]

enter image description here

$\endgroup$
27
$\begingroup$

There is an easier answer to this (esp. if you don't want to custom-specify colors), at least in version 10, found as the answer to this question:

LegendLayout -> {"Column", 1}]

All credit goes to MinHsuan Peng who answered that question, I just reposted here because I found this page first and thought other people searching for this would like to find this solution first too.

It's really disappointing (I wish I could say surprising) that this option isn't even listed in the "Possible settings for LegendLayout" in the details section of the Mathematica documentation.

$\endgroup$
1
  • $\begingroup$ Brilliant! I was so desperate finding this! $\endgroup$ Commented Jan 17, 2020 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.