1

I am having trouble fixing a value at the barlegends.

contours = Function[{min, max}, Range[min, max, Round[(max - min)/15, 0.01]]];
BarLegend[{"myColorFunction", {0.4, 1}}, contours, 
 LegendLayout -> "Row", LabelStyle -> {FontSize -> 40}, 
 LegendMarkerSize -> 1000]
BarLegend[{"myColorFunction", {0.25, 1}}, contours, 
 LegendLayout -> "Row", LabelStyle -> {FontSize -> 40}, 
 LegendMarkerSize -> 1000]

enter image description here

What I want is the first and last value to show up in the barlegends. For example, for the first one value from 0.4 to 1, it would be nice to have contour such that 0.4,0.5,0.6,0.7,0.8,0.9,1. So the first and last value is 0.4 and 1.

enter image description here

Do you have any suggestion?

Saesun Kim
  • 1,810
  • 13
  • 24

2 Answers2

3

It is puzzling that the option Ticks (or "Ticks") does not work if you specify the contours in the second argument:

bl1a = BarLegend[{"Rainbow", {0.4, 1}}, LegendLayout -> "Row", 
  LabelStyle -> {FontSize -> 40}, LegendMarkerSize -> 1000, 
  "Ticks" -> Range[.4, 1, .1]]

enter image description here

bl1b = BarLegend[{"Rainbow", {0.4, 1}}, contours, 
  LegendLayout -> "Row", LabelStyle -> {FontSize -> 40}, 
  LegendMarkerSize -> 1000, "Ticks" -> Range[.4, 1, .1]]

enter image description here

A work-around to get both contours and ticks right is to post-process the first output to replace the bar with the one from the second output:

RawBoxes[ToBoxes[bl1a] /. 
  RasterBox[___] -> Cases[ToBoxes@bl1b, _RasterBox, All][[1]]]

enter image description here

Similarly for the second example:

bl2a = BarLegend[{"Rainbow", {0.25, 1}}, LegendLayout -> "Row", 
  LabelStyle -> {FontSize -> 40}, LegendMarkerSize -> 1000, 
  "Ticks" -> Append[Range[.25, 1, .1], 1]]

bl2b = BarLegend[{"Rainbow", {0.25, 1}}, contours, LegendLayout -> "Row", LabelStyle -> {FontSize -> 40}, LegendMarkerSize -> 1000, "Ticks" -> Append[Range[.25, 1, .1], 1]]

RawBoxes[ToBoxes[bl2a] /. RasterBox[___] -> Cases[ToBoxes@bl2b, _RasterBox, All][[1]]]

enter image description here enter image description here enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
1

This answer says that you can use the undocumented Ticks option in BarLegend to control the contour labels, which I found to do the trick here.

BarLegend[{"Rainbow", {0.4, 1.}}
 , Ticks -> Subdivide[0.4, 1, 6]
 , LegendLayout -> "Row"
 , LabelStyle -> {FontSize -> 40}
 , LegendMarkerSize -> 1000]

enter image description here

BarLegend[{"Rainbow", {0.25, 1.}}
 , Ticks -> {0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95, 1}
 , LegendLayout -> "Row"
 , LabelStyle -> {FontSize -> 40}
 , LegendMarkerSize -> 1000]

enter image description here

Josh Bishop
  • 741
  • 3
  • 8