3
$\begingroup$

I have plotted a graph in mathematica, where the x axis is in seconds. I want to scale the axis so it displays years instead.

My plotting function is as follows:

Plot[sol1[t], {t, 0, 1 * 10^(16)},
   PlotRange -> All
   FrameLabel -> {"Time (s)", "Velocity (ms-1)"},
   PlotStyle -> {Red, Thickness[0.01]}]

Any help would be appreciated.

$\endgroup$
2
$\begingroup$

An alternative approach is to change the option value for FrameLabel and use FrameTicks with custom ticks:

sol[t_] := 1 + Sinc[10^-15   Pi t];

Plot[sol[t], {t, 0, 10^16}, 
   Frame -> True,  ImageSize -> Large, PlotStyle -> {Red, Thickness[0.01]}, 
   FrameLabel -> {"Time (s)", "Velocity (ms-1)"}]

enter image description here

year = 60 60 24 365;

fticks = {{Automatic, Automatic}, {Charting`FindTicks[{0, 1}, {0,  1/year}], Automatic}};


Plot[sol[t], {t, 0, 10^16}, 
   Frame -> True, ImageSize -> Large, PlotStyle -> {Red, Thickness[0.01]}, 
   FrameLabel -> {"Time (year)", "Velocity (ms-1)"}, 
   FrameTicks -> fticks]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you! This is exactly what I was looking for. Is there a documentation page explaining how Charting`FindTicks works, for example if I want to scale the y axis? I'm very new to Mathematica and not had much luck finding it. $\endgroup$ – testing09 Nov 29 '20 at 14:10
  • $\begingroup$ @testing09, the (undocumented) function Charting`FindTicks[{a, b}, {c, d}][min, max] generates the major and minor ticks based on the automatically computed minimum and maximum of the values on the axis using the scaling parameters {a, b}, {c, d} for computing the labels for major ticks. I think the way it works is ... $\endgroup$ – kglr Nov 30 '20 at 18:32
  • $\begingroup$ ... as follows: First, FindDivisions is used with inputs min, max and, some hard-coded parameter for the number of major and minor ticks ticks (which, I guess, is {6,6}) to compute major and minor ticks; then, for major tick t, Rescale[t, {a,b}, {c,d}] is used to compute the tick label for t. $\endgroup$ – kglr Nov 30 '20 at 18:33
2
$\begingroup$

For illustration purposes let sol1 be

sol1[t_] = Exp[-3*^-16 t];

The original plot

Plot[sol1[t], {t, 0, 1*10^(16)}, PlotRange -> All,
 Frame -> True, FrameLabel -> {"Time (s)", "Velocity (ms-1)"}, 
 PlotStyle -> {Red, Thickness[0.01]},
 ImageSize -> Medium]

enter image description here

The average number of seconds in a year

yr = QuantityMagnitude@UnitConvert[Quantity[1, "Years"], "Seconds"]

(* 31536000 *)

The rescaled plot

Plot[sol1[t*yr], {t, 0, 10^16/yr},
 PlotRange -> All,
 Frame -> True, 
 FrameLabel -> {"Time (yr)", "Velocity (ms-1)"},
 PlotStyle -> {Red, Thickness[0.01]},
 ImageSize -> Medium]

enter image description here

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