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}]
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 ofPlotRange
, notImageSize
.
So here is my question
What is a simple way to manually edit the ratio of the units of the axes?.
Answer
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)
]
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])]
Comments
Post a Comment