# Changing the width of BarLegend with Contour argument

Bug introduced in 10.0.2 or ealier and persisting through 11.0.1 or later

Is it possible to change the aspect ratio of the BarLegend in Mathematica?

The option LegendMarkerSize, in my case below, only changes the overall length of the bar.

So, for the example image below, how can I make the colour bar wider? Note that because of the contours argument, using LegendMarkerSize -> {300, 300} has no effect.

BarLegend[{"Rainbow", {0.0, 1.0}}, 20, LegendMarkerSize -> 300,
LegendLabel -> Style["Intensity", Bold, FontSize -> 14],
LabelingFunction -> (Style[NumberForm[#, {Infinity, 1}],
FontSize -> 12] &)]


The width of BarLegend is hard-coded to 15 printer points in the functions LegendingLegendDumpiColorGradientLegend and LegendingLegendDumpiColorBandLegend. We can post-process the BarLegend output to change this number:

ClearAll[makeWide]
makeWide[w_: 30] := RawBoxes[ToBoxes[#] /. {15/2 | 7.5 -> w/2,
-15/2 | (-7.5) -> -w/2}] &;


Examples:

bl = BarLegend[{"Rainbow", {0.0, 1.0}}, 30, LegendMarkerSize -> 400,
LegendLabel -> Style["Intensity", Bold, FontSize -> 14],
"LabelingFunction" -> (Style[NumberForm[#, {Infinity, 1}], FontSize -> 15] &)] ;

Row[{bl, makeWide[] @ bl, makeWide[60] @ bl, makeWide[100] @ bl, makeWide[400] @bl },
Spacer[20]]


If you look under the documentation for LegendMarkerSize, you will see it accepts values of the form: {w, h}. So, you can specify the width, for example

BarLegend["Rainbow", LegendMarkerSize -> {100, 100}]


• Doesn't work with the OP's case: i.stack.imgur.com/uWMqG.png. Seems like a bug to me.
– Öskå
Nov 4, 2014 at 13:56
• @Öskå its the contours (2nd argument) that is doing it. Reporting it. Nov 4, 2014 at 14:05
• I figured it was the second argument indeed, thanks for reporting it.
– Öskå
Nov 4, 2014 at 14:06
• Yes, sorry, meant to clarify but got called away - I did try these options but couldn't get it to work. Thanks! Nov 4, 2014 at 16:37