How to control the padding below a BarLegend?

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...


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.)

Tags:

Legending