5
$\begingroup$

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...

$\endgroup$

2 Answers 2

7
$\begingroup$

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

enter image description here

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

enter image description here

$\endgroup$
1
  • $\begingroup$ Ah, that makes more sense! $\endgroup$
    – Igor Rivin
    Oct 16, 2019 at 4:34
2
$\begingroup$

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

enter image description here

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

enter image description here

$\endgroup$
1
  • $\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$
    – Igor Rivin
    Oct 16, 2019 at 4:28

Your Answer

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

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