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$
  • $\begingroup$ Why not just relabelling the ticks ? $\endgroup$ – b.gates.you.know.what Jan 15 '13 at 10:07
  • $\begingroup$ How can I do this? Using... $\endgroup$ – Murta Jan 15 '13 at 10:09
  • 1
    $\begingroup$ Define pc = {#, ToString[100 #] <> " %"} & /@ Range[0, 1, 0.1] and then Ticks -> {Automatic, pc}. $\endgroup$ – b.gates.you.know.what Jan 15 '13 at 10:14
  • $\begingroup$ tks, but in this way you remove the subticks. $\endgroup$ – Murta Jan 15 '13 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 Jan 15 '13 at 10:36
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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Nice code. Tks. $\endgroup$ – Murta Jul 11 '14 at 2:18
8
$\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

| improve this answer | |
$\endgroup$
  • $\begingroup$ This theoretically works OK, but M version 8 has a bug and AbsoluteOptions doesn't work as expected for Ticks $\endgroup$ – Dr. belisarius Jan 15 '13 at 15:21
  • $\begingroup$ @belisarius Got more details on that bug? I have v8 but can't break it. $\endgroup$ – ssch Jan 15 '13 at 16:22
  • $\begingroup$ Take a look at the x-axis ticks i.stack.imgur.com/T7kbm.png $\endgroup$ – Dr. belisarius Jan 15 '13 at 16:26
  • $\begingroup$ It is v 8.0 BTW $\endgroup$ – Dr. belisarius Jan 15 '13 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 Jan 15 '13 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

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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