1
$\begingroup$

How can I set the axes of a plot to be positive in both directions, along the lines of this image? enter image description here

I've looked through some questions on how to change the axis direction (here, here and here) but they obviously don't fully answer my problem, and I've not got further than the options given in those questions. Presumably the answer lies in the Ticks function, but I'm not sure how to implement it.

Edit

I am aware of custom labels and tick positions from the documentation, but am curious about a more automatic way to do it. I was thinking along the lines of the accepted answer to How to change the axes' origin and direction?, where this is given as a way of reversing the axis direction.

x = Sin /@ Range[0, 2 π, 0.1];
ListPlot[-x, Ticks -> {Automatic, Function[{xmin, xmax}, 
    Table[{i, -i, {0.02, 0}}, {i, N@FindDivisions[{xmin, xmax}, 10]}]]}]

Is there a similar method I could use based on xmin, xmax and N@FindDivisions[], or is custom labels the way forward?

$\endgroup$
  • 1
    $\begingroup$ Have you seen the Ticks option? $\endgroup$ – Dr. belisarius May 16 '14 at 16:52
  • $\begingroup$ I have, perhaps to clarify, how would I use the ticks option to do this? $\endgroup$ – dr.blochwave May 16 '14 at 16:56
  • 1
    $\begingroup$ @blochwave Did you read the documentation on it? You need to specify tick positions with custom labels. See the docs on how, last example under Scope -> Tick positions and labeling. $\endgroup$ – Szabolcs May 16 '14 at 17:17
  • $\begingroup$ I did, I was wondering if there was a way to do it based on xmin, xmax and N@FindDivisions? As in #3 of the accepted answer here: mathematica.stackexchange.com/questions/3747/… - I've updated my question to clarify this, sorry that it wasn't clear. $\endgroup$ – dr.blochwave May 16 '14 at 17:35
3
$\begingroup$

Here is one way to do what you are asking for.

tickSpec[i_Integer] /; Mod[i, 10] == 0 := {i, Abs[i]}
tickSpec[i_Integer] := {i, ""}
With[{nTicks = 13}, 
  ticks[xmin_, xmax_] := Table[tickSpec @ i, {i, FindDivisions[{xmin, xmax}, nTicks]}]]

Plot[{}, {x, -30, 30},
  PlotRange -> {Full, {-30, 30}},
  AspectRatio -> Automatic,
  Frame -> True,
  Axes -> False,
  FrameTicks -> {{ticks, None}, {ticks, None}}]

plot

$\endgroup$
  • $\begingroup$ That's wonderful, thank you! $\endgroup$ – dr.blochwave May 17 '14 at 8:30

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.