4
$\begingroup$

Consider the following plot

Plot[{Sin[x]}, {x, 0, 2*Pi}, PlotRange -> {{0, 2*Pi}, {-1.05, 1.05}}, 
 AxesLabel -> {x, y}, AxesOrigin -> {0, 0}]

enter image description here

It is evident that $1$ unit on the $y$ axis is not as the same length of $1$ unit on the $x$ axis. I want the ratio of these units to be one or any other desired value $r=\dfrac{y \,\, \text{axis unit}}{x \,\, \text{axis unit}}$.

I searched for how to determine the scaling of these units of the axes. I encountered this post and this one. But I could not find a nice answer explaining a simple way to do the job. Also, I couldn't find a nice example in the documentation. I just learned from documentation that

AspectRatio determines the ratio of PlotRange, not ImageSize.

So here is my question

What is a simple way to manually edit the ratio of the units of the axes?.

$\endgroup$
  • $\begingroup$ Check AspectRatio option of various plotting functions... $\endgroup$ – unlikely Mar 27 '16 at 20:08
  • $\begingroup$ @unlikely: I read those parts. :) I am afraid that this is not a solution or the explanation in the documentation is not enough! :) $\endgroup$ – H. R. Mar 27 '16 at 20:09
  • 1
    $\begingroup$ Try option Plot[..., AspectRatio->Automatic] $\endgroup$ – unlikely Mar 27 '16 at 20:18
  • $\begingroup$ @unlikely: Can you kindly write an answer with proper details. I have tried these things already and do not understand what is happening! Sorry but I am a beginner! :) $\endgroup$ – H. R. Mar 27 '16 at 20:25
6
$\begingroup$

To get 1:1 unit ratio you can use

Plot[{Sin[x]}, {x, 0, 2*Pi},
 PlotRange -> {{0, 2*Pi}, {-1.05, 1.05}},
 AxesLabel -> {x, y}, AxesOrigin -> {0, 0},
 AspectRatio -> Automatic
 ]

To get for example 1:2 unit ratio you can use:

Plot[{Sin[x]}, {x, 0, 2*Pi},
 PlotRange -> {{0, 2*Pi}, {-1.05, 1.05}},
 AxesLabel -> {x, y}, AxesOrigin -> {0, 0},
 AspectRatio -> 2*(1.05 + 1.05)/(2 Pi - 0)
 ]

Mathematica graphics

As stated by @Sjoerd to define AspectRatio properly you should take into account the PlotRange as I did.

If you don't know in advance the PlotRange of your plot you can also use the following to get it after making an "hidden" plot:

g = Plot[{Sin[x]}, {x, 0, 2*Pi},
   AxesLabel -> {x, y}, AxesOrigin -> {0, 0}
   ];
Show[g, AspectRatio -> 2 / Divide @@ (Subtract @@@ PlotRange[g])]
$\endgroup$
  • $\begingroup$ This was the simple answer I was looking for! :) So the key sentence of documentation is AspectRatio determines the ratio of PlotRange, not ImageSize. $\endgroup$ – H. R. Mar 27 '16 at 20:36
  • $\begingroup$ Thanks again. The last solution is interesting. :) $\endgroup$ – H. R. Mar 27 '16 at 20:41
4
$\begingroup$
pl = Plot[{Sin[x]}, {x, 0, 2*Pi}, 
  PlotRange -> {{0, 2*Pi}, {-1.05, 1.05}}, AxesLabel -> {x, y}, 
  AxesOrigin -> {0, 0}]

Mathematica graphics

Determine the actual setting of the AspectRatio option used for this plot with AbsoluteOptions:

ar = AspectRatio /. AbsoluteOptions[pl, AspectRatio]
(* 0.618034 *)

The replacement (/.) takes care of converting the option rule to an actual number.

With the setting AspectRation->Automatic Mathematica scales this such that the units have a ratio of 1 when measured in figure dimensions:

plAutomatic = 
 Plot[{Sin[x]}, {x, 0, 2*Pi}, PlotRange -> {{0, 2*Pi}, {-1.05, 1.05}},
   AxesLabel -> {x, y}, AxesOrigin -> {0, 0}, 
  AspectRatio -> Automatic]

Mathematica graphics

The aspect ratio used in this case is:

arAutomatic = 
 AspectRatio /. AbsoluteOptions[plAutomatic, AspectRatio]
(* 0.334225 *)

Plotting the plot with this value yields the same plot as with `Automatic':

Show[pl, AspectRatio -> aut]

Mathematica graphics

Now we have a value that we can scale. Here by a factor of 2:

Show[pl, AspectRatio -> 2 aut]

Mathematica graphics

$\endgroup$
  • 1
    $\begingroup$ @H.R. I removed my comments. For 3D: BoxRatios $\endgroup$ – Sjoerd C. de Vries Mar 27 '16 at 20:52

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.