# Control the tick marks in BarLegend

Consider a BarLegend showing a smooth gradient, like this one:

SeedRandom
data = RandomReal[{-0.5, 1}, 10]

BarLegend[{"SolarColors", MinMax[data]}] I would like to have two extra tick marks showing the actual minimal and maximal values.

MinMax[data]
(* {-0.289979, 0.789597} *)


Is there a simple way to do this using BarLegend?

I guess I could just use a DensityPlot instead. Here's an example, with the CustomTicks package. But this is a bit too complicated to write up when one is in a hurry and doesn't have personal utility functions handy. Is there an easier way?

DensityPlot[y, {x, 0, 1}, {y, Min[data], Max[data]},
PlotRangePadding -> None, ColorFunction -> "SolarColors",
AspectRatio -> 15,
ImageSize -> {Automatic, 250},
FrameTicks -> {{None,
Sort@Join[
LinTicks[Min[data], Max[data], TickDirection -> Out,
MajorTickLength -> 0.2,
MinorTickLength ->
0.1], ({#, NumberForm[#, 2], {0, 0.2}, {}} &) /@
MinMax[data]]}, {None, None}}
] It looks like BarLegend takes Ticks as an undocumented option. With a helper function:

makeTicks[range_, num_] := Rescale[#, {1, num}, range] & /@ Range[num]

BarLegend[{"SolarColors", MinMax[data]}, Ticks -> makeTicks[MinMax[data], 7]] For more control, a more complicated function can be used:

makeTicks2[range_, num_] :=
MapIndexed[{#1,
NumberForm[#1, If[(#2[] == 1 || #2[] == num), 2, 1]]} &,
Rescale[#, {1, num}, range] & /@ Range[num]]

BarLegend[{"SolarColors", MinMax[data]},
Ticks -> makeTicks2[MinMax[data], 7]] • Thanks! I kept trying FrameTicks ... – Szabolcs Jan 12 '18 at 20:16

Maybe

SeedRandom
data = RandomReal[{-0.5, 1}, 10];

vals = Select[FindDivisions[MinMax@data~Join~{0.2}, 6],
IntervalMemberQ[Interval@MinMax@data, #] &]~Join~MinMax[data] //
Sort;

ticks = Transpose[{vals, NumberForm[#, 2] & /@ vals}];

BarLegend[{"SolarColors", MinMax[data]}, Ticks -> ticks] 