6
$\begingroup$

Currently my BarLegend looks like:

BarLegend[{(ColorData["Rainbow"][Rescale[#, {0, .125}]] &) , {0, .11} }]

Barlgend

I would like to add white (maybe dotted) lines to Barlegend at positions {0.02, 0.04, ...} such that it looks like

Barlegend with lines

Since I'm not only creating one plot & BarLegend, I'd also like to manually place the ticks & labels of BarLegend to make sure the ticks & labels are at the same positions as the white lines. For example: I'd like to place the ticks, labels, and white lines at {0.03, 0.06, 0.09}.

How can I achieve the lines and the custom placement?

$\endgroup$

3 Answers 3

8
$\begingroup$
incRange = 0.020;
maxRange = 0.120;

BarLegend[{"Rainbow", {0, maxRange}}, 
          Ticks -> Table[i, {i, incRange, maxRange - incRange, incRange}],  
          TickLengths -> 25, RotateTicks -> 180,   
          TicksStyle -> Directive[Opacity[1], White, Dashed, FontColor -> Black], 
          LabelStyle -> {FontSize -> 12}, LegendMarkerSize -> {20, 300}]

enter image description here

$\endgroup$
4
  • $\begingroup$ I get the BarLegend without the text. Can this be version dependent? I use Mathematica 9 and Ticks, TickLengths, RotateTicks, and TicksStyle are marked in red color. $\endgroup$
    – manifold
    Jun 17, 2016 at 6:53
  • $\begingroup$ The labels are hidden, because the barlegend isn't wide enough. Stretching the legend gives: s19.postimg.org/e50h22t5v/post3.jpg Is there any other solution? $\endgroup$
    – manifold
    Jun 17, 2016 at 7:06
  • $\begingroup$ And the labels are on the left in your solution. I'd need them on the right. $\endgroup$
    – manifold
    Jun 17, 2016 at 7:09
  • $\begingroup$ Maybe a possible solution is to not use BarLegend at all, but use another Contoutplot as the Legend. $\endgroup$
    – manifold
    Jun 17, 2016 at 7:13
5
$\begingroup$

Thank you for your post, but I now plot the Legend with a Contourplot and get the full control over all options:

plotoptions = {ColorFunction -> (ColorData["Rainbow"][
       Rescale[#, {0, .125}]] &), 
   MeshFunctions -> 
    Table[#3 &, {i, 1, Length[{0, .025, .05, .075, .1, .125}]}], 
   Mesh -> Table[{{tick, 
       Lighter[White, 
        HeavisideTheta[(tick/
            Max[{0, .025, .05, .075, .1, .125}]) - .5]]}}, {tick, {0, \
.025, .05, .075, .1, .125}[[2 ;; -2]]}], 
   MeshStyle -> {Dotted, DotDashed, Dashed, AbsoluteThickness[.5]}};

ContourPlot[y, {x, 0, 1}, {y, 0, .125}, Contours -> 100, 
 ContourLines -> False, PlotRange -> All, 
 ColorFunctionScaling -> 
  False,(*Frame\[Rule]{{False,True},{False,False}},*)
 FrameStyle -> Directive[RGBColor[.8, .8, .8], FontColor -> Black], 
 FrameTicks -> {{None, {0, .025, .05, .075, .1, .125}}, {None, None}},
  AspectRatio -> 10, ImageSize -> 50, Evaluate@plotoptions]

enter image description here

$\endgroup$
5
$\begingroup$

One can use the undocumented option "StyledContours" to add individually styled lines to a BarLegend.

BarLegend[{(ColorData["Rainbow"][Rescale[#, {0, .125}]] &), {0, .11}}, 
 "StyledContours" -> {{0.02, Directive[White, Dotted]}, {0.04, White}, {0.06, Red}, 
                      {0.08, White}, {0.1, Black}}]

Out

Ticks, labels, and white lines at a list of tick positions:

ticks = {0.03, 0.06, 0.09};
BarLegend[{(ColorData["Rainbow"][Rescale[#, {0, .125}]] &), {0, .11}}, 
 "StyledContours" -> Transpose[{ticks, ConstantArray[White, Length@ticks]}], 
 Ticks -> ticks]

Out2

$\endgroup$
1
  • $\begingroup$ thank you for introducing another potentially useful undocumented option :) $\endgroup$
    – ubpdqn
    Aug 17, 2016 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.