4
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?

Qi Zhong
  • 1,037
  • 9
  • 16

2 Answers2

7

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

Karsten7
  • 27,448
  • 5
  • 73
  • 134
3

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!

ktm
  • 4,242
  • 20
  • 28