2
$\begingroup$

I have a plot as shown, enter image description here

I want to change the axes ticks of the negative values as shown below, enter image description here

I went through this post and with this answer, I am able to do only for Y-axis. Can someone please let me know how can I do this for both axes?.

Note: I also tried the comments from this answer for X-axis but that approach is not working.

If there is any other simple ways, please let me know.

Thanks in advance :)

$\endgroup$
  • $\begingroup$ Modified Mr. Wizard's code slightly, here - seems to as you wish $\endgroup$ – Jason B. Apr 15 '16 at 9:58
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Apr 15 '16 at 9:59
  • $\begingroup$ Thanks a lot @JasonB. I didn't remove the ticks[[1]] in show function and that caused all the issues. Again thanks a lot. One more thing, If I do this, the font style between negative and positive ticks are different. Any idea how to fix that? $\endgroup$ – Selva Apr 15 '16 at 10:06
  • $\begingroup$ Never mind... Got it by adding Style[xlabels, ...] in the show function. Thanks $\endgroup$ – Selva Apr 15 '16 at 11:37
4
$\begingroup$

So this isn't as robust as I would like, but it works decently on many test cases. I wish there was a way of extracting the automatic tick placement so that you could just reverse it,

flipNegativeTicks[plot_] := 
 Module[{ticks, xticks, yticks, xlabels, ylabels, xpos, ypos, range},
  ticks = Ticks /. AbsoluteOptions[plot];
  range = Charting`get2DPlotRange@plot;
  {ypos, xpos} = Last /@ range/17;

  {yticks, ylabels} = 
   Replace[ticks[[2]], {a_?Negative, b_, c_, x__} :> {a, 
       Sow@Text[b, {ypos, a}];, -c, x}, 1] // Reap;
  {xticks, xlabels} = 
   Replace[ticks[[1]], {a_?Negative, b_, c_, x__} :> {a, 
       Sow@Text[b, {a, xpos}];, -c, x}, 1] // Reap;
  Show[plot, Graphics[{xlabels, ylabels}], Ticks -> {xticks, yticks}, 
   PlotRange -> Charting`get2DPlotRange@plot]
  ]

flipNegativeTicks@Plot[40 Sin[x/20], {x, -10, 70}]

flipNegativeTicks@Plot[Sinc[x/10000], {x, -50000, 70000}]

enter image description here

$\endgroup$
  • $\begingroup$ This is really helpful. I am beginner here, can you please help me to get tick labels as -10.0, 10.0, 20.0 etc in place -10. , 10. , 20. $\endgroup$ – souravc Oct 25 '17 at 11:20
  • $\begingroup$ @souravc - do you want to do that while also using the trick from this post, or just as a separate issue? $\endgroup$ – Jason B. Oct 25 '17 at 13:29
  • $\begingroup$ yes, when I am using the trick described in the above post I want to get tick labels as -10.0, 10.0, 20.0 etc in place -10. , 10. , 20. Should I ask a separate question? $\endgroup$ – souravc Oct 25 '17 at 13:40
  • 1
    $\begingroup$ @souravc - if you take the trick here, add the line ticks = ticks /. {a_, b_?NumericQ, c__} :> {a, NumberForm[SetPrecision[b, 4], 3], c}; after the line where ticks is defined, then the very first example plot here will do what you are looking for. This isn't versatile though, since for the second example it spits out an error because the NumberForm doesn't give enough zeros. $\endgroup$ – Jason B. Oct 25 '17 at 13:53
  • $\begingroup$ Thanks a lot. It is working for me. I will keep in mind the probable loopholes. $\endgroup$ – souravc Oct 25 '17 at 14:05
2
$\begingroup$

This is a motivating post. I'm sure others could do much better and I have not refined it (no time):

tf[n_?Positive] := Table[{j, j, {0.02, 0.02}}, {j, 1, n}]
tf[n_] := Table[{j, "", {0.02, 0.02}}, {j, n, -1}]
txth[{l_, u_}, p_, col_] := 
 Text[Style[#, col], {#, p}] & /@ Range[l, u]
txtv[{l_, u_}, p_, col_] := 
 Text[Style[#, col], {p, #}] & /@ Range[l, u]
gl[{l_, u_}, s_] := 
 Table[{j, {Gray, Thick}}, {j, l, u}]~Join~
  Table[{j, LightGray}, {j, l, u, s}]
Manipulate[
 Plot[Sin[x], {x, -6, 6}, 
  Ticks -> {tf[-6]~Join~tf[6], tf[-3]~Join~tf[3]}, 
  Epilog -> {txth[{-6, -1}, p, Red]~Join~txtv[{-3, -1}, p, Red]}, 
  GridLines -> {gl[{-6, 6}, 0.2], gl[{-4, 4}, 0.2]}, 
  AspectRatio -> Automatic, PlotRange -> {-4, 4}], {p, 0.1, 0.5}]

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.