1
$\begingroup$

BarLegend leaves a white region at the other end of the legend (the "0 end"), which is best seen by plotting a frame around the legend:

BarLegend["Rainbow", LegendFunction -> "Frame"]
BarLegend["Rainbow", LegendFunction -> "Frame", LegendLayout -> "Row"]

enter image description here

This behaviour causes that the LegendLabel is not centered with respect to the color bar (in the following plots, the label is not at the 0.5 tick). It appears that the label is centered with respect to the whole legend, which includes the white region, even when the frame is not plotted:

BarLegend["Rainbow", LegendLabel -> Placed["1", Right]]
BarLegend["Rainbow", LegendLabel -> Placed["1", Right], LegendFunction -> "Frame"]
BarLegend["Rainbow", LegendLabel -> "1", LegendLayout -> "Row"]
BarLegend["Rainbow", LegendLabel -> "1", LegendLayout -> "Row", LegendFunction -> "Frame"]

enter image description here

How can I place the LegendLabel so that it appears centered with respect to the color bar (both in horizontal and vertical BarLegends)?

When the option LegendLayout->"Row" is used, a possibility is to leave some white space before the label, but this is far from ideal:

BarLegend["Rainbow", LegendLabel -> "   1", LegendLayout -> "Row"]

enter image description here

$\endgroup$

2 Answers 2

3
$\begingroup$

For version 9, the culprit seems to be the ImagePadding settings:

bl1 = BarLegend[{"LakeColors", {0, 1}}, {0.2, 0.4, 0.5, 0.6, 0.8}, 
                LegendFunction -> "Frame", LegendLabel -> Placed["1", Right]];
bl2 = BarLegend[{"LakeColors", {0, 1}}, {0.2, 0.4, 0.5, 0.6, 0.8}, 
              LegendFunction -> "Frame", LegendLayout -> "Row", LegendLabel -> Placed["1", Top]];

Cases[ToBoxes[#], HoldPattern[ImagePadding -> _], {0, Infinity}] & /@ {bl1, bl2}
(* {{ImagePadding -> {{1, All}, {All, All}}},{ImagePadding -> {{All, All}, {All, 1}}}} *)

A work-around is to post-process the BarLegend output to change the ImagePadding values using a function like

correctPaddingF =  RawBoxes[ToBoxes[#] /. HoldPattern[ImagePadding -> p_] :>
      (ImagePadding -> (If[ p == {{1, All}, {All, All}}, {{1, All}, {1, All}}, 
                          {{1, All}, {All, 1}}]))] &; 

Row[{bl1, correctPaddingF@bl1}, Spacer[5]]

enter image description here

Row[{bl2, correctPaddingF@bl2}, Spacer[5]]

enter image description here

bl3 = BarLegend[{"LakeColors", {0, 1}}, {0.2, 0.4, 0.5, 0.6, 0.8}, 
                LegendLabel -> Placed["1", Right]];
bl4 = BarLegend[{"LakeColors", {0, 1}}, {0.2, 0.4, 0.5, 0.6, 0.8}, 
                LegendLayout -> "Row", LegendLabel -> Placed["1", Top]];

Row[{bl3, correctPaddingF@bl3}, Spacer[5]]

enter image description here

Row[{bl4, correctPaddingF@bl4}, Spacer[5]]

enter image description here

$\endgroup$
3
$\begingroup$
BarLegend["Rainbow", LegendMargins -> {{25, 10}, {0, 15}}, 
 LegendFunction -> (Framed[#, FrameMargins -> 0, 
     RoundingRadius -> 10] &)]

enter image description here

For label placement you can use

BarLegend["Rainbow", LegendMargins -> {{25, 10}, {0, 15}}, 
 LegendFunction -> (Framed[#, FrameMargins -> 0, 
     RoundingRadius -> 10] &), 
 LegendLabel -> Placed[Column[{"1", ""}, Spacings -> 0], Right]]

enter image description here

For horizontal label you can use LegendLabel -> Placed[Row[{" ", "1"}], Bottom]

$\endgroup$
1
  • $\begingroup$ Thanks. This solution provides a way to make a centered frame. However, this doesn’t affect the label placement. $\endgroup$
    – j--
    Jan 1, 2015 at 23:12

Your Answer

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

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