4

I am plotting a Bar legend for the density plot using BarLegend. I got the desired bar legend but it has ticks in the form $\frac{\pi}{6},~\frac{\pi}{3},..$, I want them to be like $\pi/6,~\pi/3,...$ I want to write the division using slash. How can I do this in mathematica.

The code I am using is:

BarLegend[{Hue[Rescale[#, {0, Pi}, {0, 1}]] &, {π/6 - .1, 2 π/3 + .1}}, 
 LabelStyle -> Directive[Black, Bold, Medium, FontFamily -> "Courier"], 
 ColorFunctionScaling -> False, 
 Ticks -> {π/6, π/3, π/2, 2 π/3}]
Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
Parveen
  • 65
  • 5
  • 3
    Hi Parveen, welcome to MMA.SE! Please consider adding some code to your question - it really helps us help you if we can quickly reproduce your results. I'm pretty sure you want something like FormatType->StandardForm! – Carl Lange Jan 21 '19 at 13:54

2 Answers2

4
ticks = {π/6, π/3, π/2, 2 π/3};
BarLegend[{Hue[Rescale[#, {0, Pi}, {0, 1}]] &, {π/6 - .1, 2 π/3 + .1}}, 
 LabelStyle -> Directive[Black, Bold, Medium, FontFamily -> "Courier"], 
 ColorFunctionScaling -> False, 
 "Ticks" -> Transpose[{#, # /. Times[Rational[a_, b_], Pi] :> Row[{a Pi, "/", b}]} &@
    ticks}], 
 LegendMarkerSize -> {30, 300}]

enter image description here

Alternatively, add the option "TickLabels":

BarLegend[{Hue[Rescale[#, {0, Pi}, {0, 1}]] &, {π/6 - .1, 2 π/3 + .1}}, 
 LabelStyle -> Directive[Black, Bold, Medium, FontFamily -> "Courier"], 
 ColorFunctionScaling -> False, "Ticks" -> ticks, 
 "TickLabels" -> (ticks /. Times[Rational[a_, b_], Pi] :> Row[{a Pi, "/", b}]), 
 LegendMarkerSize -> {30, 300}]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
4

Something like this will work:

BarLegend[{
  Hue[Rescale[#, {0, Pi}, {0, 1}]] &, {\[Pi]/6 - .1, 2 \[Pi]/3 + .1}
  },
 LabelStyle -> Directive[Black, Bold, Medium, FontFamily -> "Courier"],
 ColorFunctionScaling -> False, 
 Ticks -> ({#, InputForm@#} & /@ {\[Pi]/6, \[Pi]/3, \[Pi]/2, 
     2 \[Pi]/3})]

The idea is to pass a list like {{x1,label1},{x2,label2}...} to Ticks.

enter image description here

Since it seems like you know exactly what you want to put into your ticks, you could just put strings:

BarLegend[{Hue[Rescale[#, {0, Pi}, {0, 1}]] &, {\[Pi]/6 - .1, 
   2 \[Pi]/3 + .1}}, 
 LabelStyle -> 
  Directive[Black, Bold, Medium, FontFamily -> "Courier"], 
 ColorFunctionScaling -> False,
 Ticks -> 
  Transpose[{{\[Pi]/6, \[Pi]/3, \[Pi]/2, 2 \[Pi]/3}, {"\[Pi]/6", 
     "\[Pi]/3", "\[Pi]/2", "2\[Pi]/3"}}]]

enter image description here

Carl Lange
  • 13,065
  • 1
  • 36
  • 70