2
$\begingroup$

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] &)]

enter image description here

$\endgroup$
1
$\begingroup$

The width of BarLegend is hard-coded to 15 printer points in the functions Legending`LegendDump`iColorGradientLegend and Legending`LegendDump`iColorBandLegend. 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]]

enter image description here

$\endgroup$
6
$\begingroup$

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}]

enter image description here

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

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.