Let's define a custom colorbar
Clear["Global`*"];
colorbar[{min_, max_}, colorFunction_: Automatic, divs_: 150] :=
DensityPlot[y, {x, 0, 0.1}, {y, min, max}, AspectRatio -> 10,
PlotRangePadding -> 0, PlotPoints -> {2, divs}, MaxRecursion -> 0,
Frame -> True, FrameLabel -> {{"C", None}, {None, None}},
FrameStyle -> Thick, RotateLabel -> False,
LabelStyle -> Directive[FontFamily -> "Helvetica", 20],
FrameTicks -> {{None, ticks[min, max]}, {None, None}},
FrameTicksStyle -> Directive[FontFamily -> "Helvetica", 20, Plain],
ColorFunction -> colorFunction];
where
ticks[min_, max_] := Module[{positions = Range[min, max, (max - min)/5],
precision = Length[NestWhileList[10 # &, max/5, Floor@# != # &]] - 1,labels},
If[precision > 0,
labels = ToString /@ (NumberForm[#, {Infinity, precision}] & /@ positions)];
If[precision == 0, labels = ToString /@ Floor[positions]];
Transpose[{positions, labels}]]
valrange = {0.134512, 10.623245};
Show[colorbar[valrange, "Rainbow"]]
I want the following:
How can we modify the module ticks
, so as to be able to control the number of printing decimal digits? For example the limits in valrange
have six decimal digits, however we may want only 2.
I use version 9.0.
Any suggestions?
UPDATE:
Why the following approach do not work?
ticks2[min_, max_, dd_] := Module[{ran = Range[min, max, (max - min)/5]},
NumberForm[ran, {dd + 1, dd}]]
Clear[colorbar]
colorbar[{min_, max_}, dd_, colorFunction_: Automatic, divs_: 150] :=
DensityPlot[y, {x, 0, 0.1}, {y, min, max}, AspectRatio -> 10,
PlotRangePadding -> 0, PlotPoints -> {2, divs}, MaxRecursion -> 0,
Frame -> True, FrameLabel -> {{"C", None}, {None, None}},
FrameStyle -> Thick, RotateLabel -> False,
LabelStyle -> Directive[FontFamily -> "Helvetica", 20],
FrameTicks -> {{None, ticks2[min, max, dd]}, {None, None}},
FrameTicksStyle -> Directive[FontFamily -> "Helvetica", 20, Plain],
ColorFunction -> colorFunction];
valrange = {0.123, 10.232};
Show[colorbar[valrange, 3, "DeepSeaColors"]]