4
$\begingroup$

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}]
$\endgroup$
  • 3
    $\begingroup$ 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! $\endgroup$ – Carl Lange Jan 21 at 13:54
5
$\begingroup$
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

$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – Parveen Jan 21 at 14:51
  • $\begingroup$ @Parveen, it could be related to version/os. I am using version 11.3 (windows 10). $\endgroup$ – kglr Jan 21 at 14:57
  • $\begingroup$ Seems so. I am using ubuntu and mathematica 11 $\endgroup$ – Parveen Jan 21 at 15:00
  • $\begingroup$ @Parveen, mathematica 11.3 or an earlier version? $\endgroup$ – kglr Jan 21 at 15:01
  • $\begingroup$ 11.1.0.0 version $\endgroup$ – Parveen Jan 21 at 15:02
4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Parveen Jan 21 at 14:53
  • $\begingroup$ @Parveen Check my latest edit. $\endgroup$ – Carl Lange Jan 21 at 15:10
  • $\begingroup$ Thanks. It works now $\endgroup$ – Parveen Jan 21 at 15:12
  • $\begingroup$ Consider marking the answer (or @kglr's answer, which is at least as good as mine) as accepted if it solved your problem! $\endgroup$ – Carl Lange Jan 21 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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