In an earlier post, I asked how one can change the ratio of the units of the axes. The easiest way to set the ratio to one was

1- setting a PlotRange and
2- using the option AspectRatio->Automatic

However, I couldn't find any easy way to make the tick-marks of both axes the same! I will explain why I want such a thing.

For example, consider the following plot. As you may see, the ratio of units of the axes is one but tick marks are not the same and this deceives the observer, suggesting that it may not be a $1:1$ ratio.

So how can I make the tick marks of the $x$ axis the same as the ones for the $y$ axis, or vice versa, in the most economic way?

  f = Piecewise[{{1, 0 <= x <= 2}, {2, 2 < x <= 4}}]
  P = Plot[f, {x, 0, 4}, PlotRange -> {{0, 4}, {0, 2.5}}, 
        AspectRatio -> Automatic, ImageSize -> Medium]

enter image description here


The built-in ticks functions are automatically passed the range for the axis being "ticked off." One can override this to get uniform ticking (if that's what it's called).

fixedTicks[min_, max_] := Charting`ScaledTicks[{Identity, Identity}][min, max, ##3] &;

foo = Plot[Piecewise[{{1, 0 <= x <= 2}, {2, 2 < x <= 4}}], {x, 0, 4},
  Ticks -> {fixedTicks[0, 4], fixedTicks[0, 4]},
  PlotRange -> {{0, 4}, {0, 2.5}}, AspectRatio -> Automatic, 
  ImageSize -> Medium]

Mathematica graphics

  • $\begingroup$ (+1) Just as beginner, how do you find this stuff in the documentation? :) I just read the whole page about Ticks and I couldn't find the thing you mentioned here! :) $\endgroup$ – Hosein Rahnama Jun 19 '16 at 21:14
  • $\begingroup$ and BTW, I really don't understand how the things are working in fixedTicks function as it is written in Pro style so that a Noob cannot get it ! :D Would you add some explanation for that or write in a simpler form, please? $\endgroup$ – Hosein Rahnama Jun 19 '16 at 21:17
  • 3
    $\begingroup$ @H.R. You write fixedTicks[min, max], where min is the smallest value and max is the largest value. As for the rest, ScaledTicks is undocumented and limited knowledge is available (i.e., I don't fully understand it - I learn some things by imitation). See this answer and others; the funny signs (links here) are required, I think, (i.e., no simpler code), and a Noob just has to be patient and learn them to fully understand. HTH $\endgroup$ – Michael E2 Jun 19 '16 at 23:37

Explicitly specifying the tick positions may be the simplest way of getting what you want. E.g.

Plot[f, {x, 0, 4}, PlotRange -> {{0, 4}, {0, 2.5}},
   AspectRatio -> Automatic, ImageSize -> Medium, 
   Ticks -> {Range[0, 4, 0.5], Range[0, 2.5, 0.5]}]
  • $\begingroup$ (+1), Nice, but how about having major and minor ticks? $\endgroup$ – Hosein Rahnama Jun 19 '16 at 18:14
  • $\begingroup$ Mathematica help for Tick provides an example where this is done reference.wolfram.com/language/ref/Ticks.html# under "Applications" $\endgroup$ – mikado Jun 19 '16 at 18:21
  • 1
    $\begingroup$ @H. R. you might find FindDivisions[] to be a handy function in that context. $\endgroup$ – J. M.'s ennui Jun 19 '16 at 18:53

Here is another solution that I found useful. I think that it has more compatibility in comparison with other answers and it is easy to understand for a beginner.

TickMark[min_, max_, Inc_] := Table[
    {i, i, .02, Black},
    {i, Null, 0.01, Black}],
   {i, Floor[min], Ceiling[max], Inc}]
f[x_] := Piecewise[{{1, 0 <= x <= 2}, {2, 2 < x <= 4}}]
xAxis = TickMark[0, 4, 1/5];
yAxis = TickMark[0, 5/2, 1/5];
T = {xAxis, yAxis};
P = Plot[{f[x]}, {x, 0, 4}, PlotRange -> {{0, 4}, {0, 5/2}}, 
  AspectRatio -> Automatic, Ticks -> T, ImageSize -> Medium]

enter image description here


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.