2

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

dr.blochwave
  • 8,768
  • 3
  • 42
  • 76

2 Answers2

10

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

rcollyer
  • 33,976
  • 7
  • 92
  • 191
2

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

kglr
  • 394,356
  • 18
  • 477
  • 896