# How can I replace division sign with slash in ticks of BarLegend

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}]

• 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! Jan 21 '19 at 13:54

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}]


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}]


• Thanks for the answer. Something strange happened. I did copy and paste your code into my mathematica 11 and the ticks got interchanged. However I got the plot as you got but in my case $\pi/6$ appears instead of $\pi/2$. I don't know why! Jan 21 '19 at 14:51
• @Parveen, it could be related to version/os. I am using version 11.3 (windows 10).
– kglr
Jan 21 '19 at 14:57
• Seems so. I am using ubuntu and mathematica 11 Jan 21 '19 at 15:00
• @Parveen, mathematica 11.3 or an earlier version?
– kglr
Jan 21 '19 at 15:01
• 11.1.0.0 version Jan 21 '19 at 15:02

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.

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"}}]]


• Seems correct but I want $\pi$ instead of $Pi$ and the upper most tick should be $2\pi/3$ instead of $2*Pi/3$. If you can modify it, then it would be a great help Jan 21 '19 at 14:53
• @Parveen Check my latest edit. Jan 21 '19 at 15:10
• Thanks. It works now Jan 21 '19 at 15:12
• Consider marking the answer (or @kglr's answer, which is at least as good as mine) as accepted if it solved your problem! Jan 21 '19 at 15:22