3
$\begingroup$
BarLegend["LakeColors", {0.2, 0.4, 0.6, 0.8}]

I want to display Pi in the BarLegend. I can do similar thing in FrameTicks like this

FrameTicks->{0, {Pi/4, "\[Pi]/4"}, {Pi/2,"\[Pi]/2"}, {3 Pi/4, "3\[Pi]/4"}, {Pi, "\[Pi]"}};

But it doesn't work in BarLegend. How to solve it?

$\endgroup$
  • $\begingroup$ Hmm.. Unfortunately the documentation doesn't make it look immediately possible with a standard piece of functionality, but maybe it's possible with some finnicking. $\endgroup$ – ktm Oct 28 '16 at 2:51
  • $\begingroup$ related q/a: How to add own description to BarLegend? $\endgroup$ – kglr Oct 28 '16 at 16:24
5
$\begingroup$

One can use the undocumented option Ticks instead of FrameTicks.

BarLegend[{"LakeColors", {0, 2}}, 
 Ticks -> {0, {Pi/4, "π/4"}, {Pi/2, "π/2"}, {3 Pi/4, "3π/4"}, {Pi, "π"}}]

Out

| improve this answer | |
$\endgroup$
3
$\begingroup$

Write your own, for example:

dat = Table[{ColorData["LakeColors", n], 
    Rectangle[{0, n}, {.2, n + 1/3}]}, {n, 0, 1, 1/3}];
labels = Table[{GrayLevel[.5], Line[{{0, n*4/3}, {.3, n*4/3}}], Black,
     Inset[Rationalize[n]*Pi, {.4, n*4/3}]}, {n, 0, 1, .25}];
myLegend = Graphics[{dat, labels}, ImageSize -> 50];
Labeled[ContourPlot[Sin[x  y], {x, 0, \[Pi]}, {y, 0, \[Pi]}, 
  Contours -> 3, ColorFunction -> "LakeColors"], myLegend, Right]

enter image description here

I do concede this is not immediately as pretty as the default (you can adjust that though), and not nearly as easy, but it offers more customization. And if customizing your tick labels isn't possible (who knows) it may be a good option.

You can add any other graphics you'd like as well, i.e. more borders, more ticks, etc...

I hope someone comes along and provides a better solution, however!

| improve this answer | |
$\endgroup$

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