Mathematica Asked by nanjun on April 17, 2021
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?
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}
]
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}
]
Correct answer by MarcoB on April 17, 2021
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]]
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]]
fractionToRow @ Style[Pi^(I/2)/(I + E/Pi), 24]
For more complicated inputs more work may be needed for appropriate parsing.
Answered by kglr on April 17, 2021
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]
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 ;)
Answered by Nasser on April 17, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP