8

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?.

Hosein Rahnama
  • 1,727
  • 1
  • 15
  • 29

2 Answers2

8

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])]
unlikely
  • 7,103
  • 20
  • 52
6
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

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323