7
$\begingroup$

To create a percentage axe in some plot I use this Ticks configuration:

ListPlot[
     Range[0,1,0.1]
    ,Ticks->{Automatic, {#,If[IntegerPart@Mod[100#,10]==0,Row[{100#,"%"}],""]}&/@Range[0,1,0.05]}
]

enter image description here

That has 3 disadvantage:

  1. Non automatic range scale
  2. No subticks with different sizes
  3. It's nor elegant nor simple

I miss a Mathematica TicksFunction or equivalent, so I could have better control over it. I also tried something like: ListPlot[Quantity[Range[0, 1, 0.1], "Percent"]] without succes.

Some clue into a better approach?

$\endgroup$
8
  • $\begingroup$ Why not just relabelling the ticks ? $\endgroup$ Commented Jan 15, 2013 at 10:07
  • $\begingroup$ How can I do this? Using... $\endgroup$
    – Murta
    Commented Jan 15, 2013 at 10:09
  • 1
    $\begingroup$ Define pc = {#, ToString[100 #] <> " %"} & /@ Range[0, 1, 0.1] and then Ticks -> {Automatic, pc}. $\endgroup$ Commented Jan 15, 2013 at 10:14
  • $\begingroup$ tks, but in this way you remove the subticks. $\endgroup$
    – Murta
    Commented Jan 15, 2013 at 10:27
  • 1
    $\begingroup$ Would it fit to your approach to use the label, i.e. somethin like AxesLabel -> {"your x-axis label", "something in %"} and leave the TicksLabels` as they are (and hence keep the MinorTicks ? $\endgroup$
    – Ronny
    Commented Jan 15, 2013 at 10:36

3 Answers 3

2
$\begingroup$

In Mathematica 9, I developed a PercentTicks function to solve this problem. Here is an example of how it is used:

ListPlot[Table[{x, x}, {x, 0, .003, 0.0001}], 
         Ticks -> { Automatic, PercentTicks }]

which gives:

PercentTicks example

The code is in my github repo.

$\endgroup$
1
  • $\begingroup$ Nice code. Tks. $\endgroup$
    – Murta
    Commented Jul 11, 2014 at 2:18
9
$\begingroup$

Replace the automatically generated labels to not have to care about generating the ticks yourself and preserve subticks:

percentTicks[plot_] := Module[{xticks, yticks},
  {xticks, yticks} = (Ticks /. AbsoluteOptions[plot, Ticks]);
  yticks = 
   yticks /. {y_, lbl_, rest__} /; lbl != "" :> {y, ToString[y*100] <> "%", rest};
  Show[plot, Ticks -> {xticks, yticks}]
  ]

percentTicks@ListPlot[Sort@RandomReal[{0, 1}, {50}]]

plot

$\endgroup$
8
  • $\begingroup$ This theoretically works OK, but M version 8 has a bug and AbsoluteOptions doesn't work as expected for Ticks $\endgroup$ Commented Jan 15, 2013 at 15:21
  • $\begingroup$ @belisarius Got more details on that bug? I have v8 but can't break it. $\endgroup$
    – ssch
    Commented Jan 15, 2013 at 16:22
  • $\begingroup$ Take a look at the x-axis ticks i.sstatic.net/T7kbm.png $\endgroup$ Commented Jan 15, 2013 at 16:26
  • $\begingroup$ It is v 8.0 BTW $\endgroup$ Commented Jan 15, 2013 at 16:26
  • $\begingroup$ It's not simple, but I agree it's the best that can be done with MMA until version 9. Tks! 1+ $\endgroup$
    – Murta
    Commented Jan 15, 2013 at 19:53
3
$\begingroup$

You can take control of all relevant bits :

pcTicks = {#, ToString[100 #] <> " %", {0.02, 0}} & /@  Range[0, 1, 0.1];
pcSubTicks = {#, "", {0.01, 0}} & /@ (Range[0, 1, 0.1] + 0.05);

ListPlot[Range[0, 1, 0.1], Ticks -> {Automatic, Join[pcTicks, pcSubTicks]}]

enter image description here

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