Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

The legending functionality in versions 9 and higher is much easier to use than the PlotLegends package and other bespoke ways of creating legends in earlier versions. However, the vertical spacing between items is larger than I would like. How can I reduce this spacing?

Changing the LegendMarkerSize option to a smaller number without changing the size specified in the LegendMarkers option just ends up cutting off the graphic. LegendMargins doesn't affect the inter-item spacing, only the space between the whole legend and other items.

PointLegend[{RGBColor[1, 0, 0], RGBColor[0, 0, 1]}, {"Series 1", "Series 2"}, 
 LegendMarkers -> {{"\[FilledCircle]", 20}, {"\[FilledCircle]", 20}}, 
 LegendMarkerSize -> 19, 
 LabelStyle -> {FontFamily -> "Arial", FontSize -> 20}] 

enter image description here

share|improve this question

2 Answers 2

up vote 15 down vote accepted

It turns out that the Spacings option does exactly what is needed, even though its use in legend constructs such as PointLegend is not documented, and it shows as red text when you use it in those constructs.

PointLegend[{Red, Blue}, {"Series 1", "Series 2"}, 
 LegendMarkers -> {{"\[FilledCircle]", 20}, {"\[FilledCircle]", 20}}, 
 Spacings -> {0.2, 0.2}, LegendMarkerSize -> 19, 
 LabelStyle -> {FontFamily -> "Arial", FontSize -> 20}]

enter image description here

This continues the tradition of undocumented options and functions in Mathematica.

UPDATE: the Spacings option is still marked with red text in version 10.0.1.

share|improve this answer
9  
I wished WRI would do once a "documentation release": no new features, just existing ones better, much better,documented. –  Rolf Mertig Jan 9 '13 at 22:50

Another option, which I use now, is to use Grid. With Grid you have all the options that comes with Grid and hence you can build the legend in as many different ways as you wish.

Manipulate[

 plotLegend = 
  Placed[LineLegend["Expressions", LegendMargins -> 1, 
    LegendLayout -> Function[{x},
      Grid[x, Spacings -> {hspacing, vspacing}, Alignment -> Left, 
       Frame -> isFrame]]], location];

 Plot[{Sin[x], Cos[x]}, {x, -Pi, Pi}, PlotLegends -> plotLegend, 
  ImageSize -> 300, ImageMargins -> 50, Frame -> True],
 Grid[{
   {"vertical spacing", 
    Control[{{vspacing, .1, ""}, 0, 3, .01, Appearance -> "Labeled"}]},
   {"horizontal spacing", 
    Control[{{hspacing, .1, ""}, 0, 3, .01, Appearance -> "Labeled"}]},
   {"add frame?", Checkbox[Dynamic[isFrame]], , SpanFromLeft},
   {"location?", PopupMenu[Dynamic[location, (location = #) &],
     {
      {Right, Top} -> Style["{Right,Top}", 12],
      Right -> Style["Right", 12],
      Left -> Style["Left", 12],
      {Right, Bottom} -> Style["{Right,Bottom}", 12],
      Above -> Style["Above", 12],
      Below -> Style["Below", 12]
      }], SpanFromLeft
    }
   }, Spacings -> {0, 1}, Alignment -> Left],
 {{location, {Right, Top}}, None},
 {{isFrame, True}, None}
 ]

Mathematica graphics

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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