5
$\begingroup$

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"]]
$\endgroup$
2
$\begingroup$

Using as much as you originally had, this works:

Clear["Global`*"];

colorbar[{min_, max_, prec_}, 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, prec]}, {None, None}}, 
   FrameTicksStyle -> Directive[FontFamily -> "Helvetica", 20, Plain],
    ColorFunction -> colorFunction];

ticks[min_, max_, prec_] := 
 Module[{positions = Range[min, max, (max - min)/5], 
   precision = 
    Length[NestWhileList[10 # &, max/5, Floor@# != # &]] - 1, labels},
   If[precision > 0, 
   labels = 
    ToString /@ (NumberForm[#, {Infinity, prec}] & /@ positions)];
  If[precision == 0, labels = ToString /@ Floor[positions]];
  Transpose[{positions, labels}]]

valrange = {0.134512, 10.623245, 3};
Show[colorbar[valrange, "Rainbow"]]

enter image description here

$\endgroup$
7
$\begingroup$

WL Version 11.2 has new function: DecimalForm. It allows enforcing of decimal form and control of total and after-decimal-point numbers of digits.

Control total number of digits:

DecimalForm[N@Pi, 5]

3.1416

Reduce number of digits after-decimal-point

DecimalForm[N@Pi, {5, 3}]

3.142

...or increase

DecimalForm[N@Pi, {5, 7}]

3.1416000

$\endgroup$
1
$\begingroup$

Why not minimize the digits shown by using the default ticks function? For example:

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,Charting`ScaledTicks["Linear"]},{None,None}},
    FrameTicksStyle->Directive[FontFamily->"Helvetica",20,Plain],
    ColorFunction->colorFunction
];

Then:

valrange={0.134512,10.623245};

Show[colorbar[valrange,"Rainbow"]]

enter image description here

$\endgroup$
  • $\begingroup$ Nice, but I want the specific ticks module, for printing always 6 ticks. $\endgroup$ – Vaggelis_Z Jan 8 '18 at 20:59

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.