11
$\begingroup$

Take this plot for instance:

enter image description here

It shows power in W, but I'd like to see it in µW, so that I get 200, 400, 600,..., which is much more readable. Is there a way to do this without scaling my function and PlotRange by a factor 10$^6$?

edit re szabolcs's comment
I already tried

Ticks -> {Automatic, Table[200 x, {x, 5}]}  

but that seems to put those tick marks where they belong, namely at 200 W, 400 W, etc.(which is 240 km up the graph), not at the gridlines.

$\endgroup$
4
  • 1
    $\begingroup$ Take a look at the Ticks or FrameTicks option, and specify ticks manually. $\endgroup$
    – Szabolcs
    Jul 16, 2012 at 8:39
  • 1
    $\begingroup$ Also see this question $\endgroup$ Jul 16, 2012 at 8:42
  • 1
    $\begingroup$ You need to specify the label and position separately. Take a look at the Ticks docs. I realize this is an inconvenient way, but unfortunately I don't have a better suggestion. $\endgroup$
    – Szabolcs
    Jul 16, 2012 at 8:48
  • $\begingroup$ Loosely related: About number truncation of ticks display in ListPlot $\endgroup$
    – Yves Klett
    Jul 16, 2012 at 10:01

3 Answers 3

10
$\begingroup$

The direct way to change the labels would be to replace them.

pl = Plot[Sin[x], {x, 0, 10}];
newTicks = AbsoluteOptions[pl, Ticks][[1, 2, 1]] /. 
     {n_, lab_?NumberQ, rest__} -> {n, 1000 lab, rest};
Show[pl, Ticks -> {newTicks, Automatic}]

?NumberQ asserts that only labeled ticks are handled.

Mathematica graphics

$\endgroup$
9
$\begingroup$

To achieve this, you need to set the Ticks (or FrameTicks) option explicitly, and specify the tick positions (coordinates) and labels separately.

I'll show you how to do it using the CustomTicks` package (part of LevelScheme`), as I find this more convenient and flexible.

<< CustomTicks`

μticks = LinTicks[#1, #2, TickLabelFunction -> Function[x, 1000 x]] &

Plot[Sin[x], {x, 0, 10}, Ticks -> {μticks, Automatic}]

Mathematica graphics

When using the μticks custom tick function I defined, the labels show 1000 times the actual value of the function. Note that major (labelled) and minor ticks are nicely preserved without needing to do a lot of work. This is the advantage the CustomTicks` package gave me. Otherwise I would have had to generate these by hand.

It is based on the LinTicks function from the CustomTicks` package, which automatically generates ticks very similar to Mathematica's default. I used its TickLabelFunction option to generate labels that are different from the actual label positions. I was too lazy to get rid of the decimal points in the labels when they're not necessary.

Please see the docs on allowed values of the Ticks option. I chose to pass it a function which generates the ticks based on the upper and lower bounds.

$\endgroup$
1
  • 1
    $\begingroup$ I agree that the CustomTicks` package is the easiest (+1). A related post (with additional installation instructions) is "Styling ticks in a plot" $\endgroup$
    – Jens
    Jul 16, 2012 at 16:12
4
$\begingroup$

Kudos to Peter - by investigating his solution, I made my own solution that might be a little more robust. The only problem with Peter's solution is that it messes up if the variables n, lab, and rest are already assigned.

Here's the function I wrote:

TickScale[plotname_, factor_] := 
 Map[Times[#, {1, If[NumberQ[#[[2]]], 1/factor, 1], {1, 1}, {1, 1}}] &,
  AbsoluteOptions[plotname, Ticks][[1, 2, 1]]  ]

and here's an example of how to use it:

f[x_] := Exp[-(x - 10^6)^2/10^5]
p1 = Plot[f[x], {x, 10^6 - 2000, 10^6 + 2000}, PlotRange -> All];
Show[p1, Ticks -> {TickScale[p1, 10^6], Automatic}, 
 AxesLabel -> {"x ( \[Times] 10^6 )", None}]

Comparison of the original plot and the plot with scaled tick marks:

ScaledTicks example output

Notice that the Mathematica is actually able to show more tick labels in the second plot since the labels are more compact.

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