9
$\begingroup$

BarLegend has an annoyingly large space at the bottom. I can see that this was added to make it line up with the frame of the plot when there is a bottom frame label (see e.g. here), but this does not work out well for all plot sizes, and it causes problems in specific situations.

How can I control this space?

Select the legend to see the spacing better:

enter image description here

This space is also much too large for a legend label placed below the legend.

Row[
 {Framed@BarLegend["Rainbow", 
    Method -> {ImagePadding -> None, ImageMargins -> None, 
      PlotRangePadding -> None}, LegendMargins -> 0, 
    LegendLabel -> "foo"],
  Framed@BarLegend["Rainbow", 
    Method -> {ImagePadding -> None, ImageMargins -> None, 
      PlotRangePadding -> None}, LegendMargins -> 0, 
    LegendLabel -> Placed["foo", Below]]}
 ]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Disgustingly, you can pass negative values to LegendMargins: BarLegend["Rainbow", LegendMargins -> {{0, 0}, {-10, 0}}] $\endgroup$ – Carl Lange Oct 30 '19 at 11:42
  • $\begingroup$ @CarlLange Excellent! Post answer! $\endgroup$ – Szabolcs Oct 30 '19 at 11:44
7
$\begingroup$

You can pass negative values to LegendMargins like so:

BarLegend["Rainbow", LegendMargins -> {{0, 0}, {-15, 0}}]

enter image description here

Happily, this appears to work quite stably, even though it feels wrong:

Row[{Framed@
   BarLegend["Rainbow", 
    Method -> {ImagePadding -> None, ImageMargins -> None, 
      PlotRangePadding -> None}, LegendMargins -> {{0, 0}, {-15, 0}}, 
    LegendLabel -> "foo"], 
  Framed@BarLegend["Rainbow", 
    Method -> {ImagePadding -> None, ImageMargins -> None, 
      PlotRangePadding -> None}, LegendMargins -> {{0, 0}, {-15, 0}}, 
    LegendLabel -> Placed["foo", Below]]}]

enter image description here

I am a little disappointed that the "zero-point" still has such a large margin on it - having to pass negative values feels like a hack. I almost wish it didn't work so the margin could be called a bug...

| improve this answer | |
$\endgroup$
  • $\begingroup$ It's really ugly, but I was not able to find any other solution, and I'm happy that it moves things forward. $\endgroup$ – Szabolcs Oct 30 '19 at 12:01
  • 1
    $\begingroup$ (+1) If we replace LegendLabel -> "foo" with LegendLabel["foo", Above], then we can use slightly simpler form Row[Framed@ BarLegend["Rainbow", LegendMargins -> {{0, 0}, {-15, 0}}, LegendLabel -> Placed["foo",#]]&/@{Above, Below}] without the need for Method suboptions. $\endgroup$ – kglr Oct 30 '19 at 15:38
3
$\begingroup$

You can also use a custom LegendFunction (to inject the option ImagePadding -> {{Automatic, Automatic}, {0, Automatic}} to the graphics object produced by BarLegend)

ClearAll[lgF]
lgF = Show[Cases[#, _Graphics, All],
  ImagePadding -> {{Automatic, Automatic}, {0, Automatic}}]&;

Row[Framed @ BarLegend["Rainbow", 
   LegendLabel -> Placed["foo", #], 
   LegendFunction -> lgF] & /@ {Above, Below}]

enter image description here

Somehow, using the same option as a Method sub-option does not work. (Apparently, it is overridden during processing.)

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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