Suppose I have a graph with y values between 0 and 1 (say). How would I go about changing the tick labels to percentage (so that 0.1 would go to 10%, and so on)? Documentation seems silent on this point...
2 Answers
You can use PercentForm
to express tick labels in percents:
ticks = {#, ToString[PercentForm[#]]} & /@ Range[0, 1, .1];
Plot[x, {x, 0, 1}, Ticks -> {Automatic, ticks }]
Alternatively, you can use Quantity[x, "Percent"]
to get x%
:
ticks2 = {#, ToString[Quantity[100 #, "Percent"], StandardForm]} & /@ Range[0, 1, 1/10];
Plot[x, {x, 0, 1}, Ticks -> {Automatic, ticks2}]
same picture
Update: To label only major ticks:
tickF = Charting`ScaledTicks["Linear"][##] /.
{a_, _NumberForm, b___} :> {a, ToString @ PercentForm[a], b} &;
Plot[x, {x, 0, 1}, Ticks -> {Automatic,tickF}]
You can add the option
Ticks -> {Automatic, Transpose@{Range[0., 1., 0.1], ToString[#] <> "%" & /@ Range[0, 100, 10]}}
For example
Plot[x, {x, 0, 1}, Ticks -> {Automatic, Transpose@{Range[0., 1., 0.1], ToString[#] <> "%" & /@ Range[0, 100, 10]}}]
Or for a more general range of data we can use a function in Ticks
percentticks[ymin_, ymax_] := Transpose@{Range[ymin, ymax, (ymax - ymin)/10.], ToString[#] <> "%" & /@ Range[0, 100, 10]}
Plot[x, {x, 0, 10}, Ticks -> {Automatic, percentticks}]
-
$\begingroup$ This certainly works, but not exactly transparent. Since Mathematica supports a bunch of number formats, one might thing %ages would be the first thing that came to mind (especially as there is AccountingForm). But noooo... $\endgroup$ Oct 16, 2019 at 4:28