8
$\begingroup$

Is there any option to show the fraction numbers in the tick labels of a plot in the format of 2/3 instead of using a horizontal bar as the fraction line?

$\endgroup$
1

3 Answers 3

8
$\begingroup$

You could specify a custom label for each tick, programmatically generated using the InputForm of the fraction, which uses the horizontal dash. For instance:

Plot[
 x, {x, 0, 3},
 Ticks -> {{#, InputForm[#]} & /@ Range[0, 3, 1/3], Automatic}
]

with axes


To address your comment regarding the dropped labels, you may be better off using a Frame and FrameLabels instead:

Plot[
 x, {x, 0, 3},
 Frame -> {{True, False}, {True, False}},
 FrameTicks -> {{#, InputForm[#]} & /@ Range[0, 3, 1/3], Automatic}
]

with frame

$\endgroup$
4
  • $\begingroup$ Thank you. I noticed that Mathematica sometimes omits some tick labels, which is also the case in your plot above. Is there a way of letting it not omit any tick labels if they are given in a list, e.g. the 0 in your list Range[0, 3, 1/3]? $\endgroup$
    – nanjun
    Commented Jan 27, 2017 at 22:21
  • $\begingroup$ @nanjun Yes, but perhaps a frame would be easier to use in that case; see my updated answer. $\endgroup$
    – MarcoB
    Commented Jan 27, 2017 at 22:27
  • $\begingroup$ I wonder what is the criterion that Mathematica drops labels? Using frame is a way around but it shows a little bit more before 0. Is it possible to disable it from dropping labels? $\endgroup$
    – nanjun
    Commented Jan 27, 2017 at 22:31
  • 3
    $\begingroup$ @nanjun It drops that $0$ label because, when axes are present, it would be positioned at the axis intersection point, and it would be awkwardly placed. I don't know of a simple way of forcing its placement. Additionally, the amount of padding on each axis (i.e. the amount of "extra space" you were referring to) should be exactly the same whether you are using axes or frames, and is controlled by PlotRangePadding which for Plot is set as 2% by default (i.e. Scaled[0.02]), if I remember correctly. $\endgroup$
    – MarcoB
    Commented Jan 27, 2017 at 22:34
9
+200
$\begingroup$
ClearAll[fractionToRow]
fractionToRow = RawBoxes @* 
   ReplaceAll[FractionBox -> (RowBox[{#, "/", #2}] &)] @* ToBoxes @* TraditionalForm

plt1 = Plot[Sin@x, {x, 0, 2 Pi}, ImageSize -> 300, 
   Ticks -> {Range[0, 2 Pi, Pi/2], Automatic}];

plt2 = Plot[Sin@x, {x, 0, 2 Pi}, ImageSize -> 300, 
   Ticks -> {{#, fractionToRow @ #} & /@ Range[0, 2 Pi, Pi/2], Automatic}];

Row[{plt1, plt2}, Spacer[10]]

enter image description here

This approach works for simple inputs (as is usually the case for tick labeling).

Another example:

TraditionalForm[Style[Pi^(I/2)/ (I + E/Pi), 24]]

enter image description here

fractionToRow @ Style[Pi^(I/2)/(I + E/Pi), 24]

enter image description here

For more complicated inputs more work may be needed for appropriate parsing.

$\endgroup$
1
  • $\begingroup$ @Szabolcs, thank you for the generous bounty. $\endgroup$
    – kglr
    Commented Apr 14, 2020 at 5:25
8
$\begingroup$

If one is allowed to use a package, here is another possibility. First load MaTeX then

SetOptions[MaTeX, "Preamble" -> {"\\usepackage{nicefrac}"}];
makeFrac[n_] := If[Denominator[n] == 1, MaTeX[n, Magnification -> 1.5], 
   MaTeX["\\nicefrac{" <> ToString[Numerator[n]] <> "}{" <> 
     ToString[Denominator[n]] <> "}", Magnification -> 1.5]];

texStyle = {FontFamily -> "Latin Modern Roman", FontSize -> 14};

Plot[x, {x, 0, 3}, 
 Ticks -> {{#, makeFrac[#]} & /@ Range[0, 3, 1/3], Automatic}, 
 BaseStyle -> texStyle]

Mathematica graphics

I would do both axes using Latex myself and not one to make it look better. And I think the fractions look better than using Mathematica's own typesetting ;)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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