Related to this question in a way. After making a plot, I'd like to get the plot range (not the range of the function, but the range chosen for plotting which usually leaves some buffer room). I've tried Plot[Sin[x], {x,0,10}]//PlotRange
but that instead gives what seems to be the range of the function.
$\begingroup$
$\endgroup$
7
2 Answers
$\begingroup$
$\endgroup$
Your method works just find to get the PlotRange
, but now you want to know what the PlotRangePadding
is.
AbsoluteOptions[Show[Plot[Sin[x], {x, 0, 10}]], PlotRange]
AbsoluteOptions[Show[Plot[Sin[x], {x, 0, 10}]], PlotRangePadding]
(* {PlotRange -> {{0., 10.}, {-0.999999, 1.}}} *)
(* {PlotRangePadding -> {{Scaled[0.02],
Scaled[0.02]}, {Scaled[0.05], Scaled[0.05]}}} *)
This gives the answer in terms of a scaled value, so we can write a function to get the full padded range (edited to test whether the padding is in fact scaled),
padRange[range_, padding_] := Module[{scales},
If[SameQ[Head[padding[[1]]], Scaled],
scales = padding[[All, 1]];
range + {-1, 1} scales First@Differences[range]
, range + padding]
]
fullRange[plot_] :=
Module[{prange, plotrangepadding, paddingscales, unscaledpadding},
prange = plot // PlotRange;
plotrangepadding = AbsoluteOptions[plot, PlotRangePadding][[1, 2]];
padRange @@@ Transpose@{prange, plotrangepadding}
]
fullRange /@ {Plot[Sin[x], {x, 0, 10}],
Plot[Sin[x], {x, 0, 10}, PlotRange -> {-2, 2}],
Plot[Sin[x], {x, 0, 10}, PlotRange -> All],
Plot[Sin[x], {x, 0, 10}, PlotRange -> {{-20.322, 2200}, {-2, 2}}]
}
(* {{{-0.2, 10.2}, {-1.1, 1.1}},
{{-0.2, 10.2}, {-2., 2.}},
{{-0.2, 10.2}, {-1.1, 1.1}},
{{-20.322, 2200.}, {-2., 2.}}} *)
This shows that if you explicitly set the PlotRange
, then no padding takes place
$\begingroup$
$\endgroup$
Try this:
Select[(Plot[Sin[x], {x, 0, 10}] // Options), #[[1]] == PlotRange &]
(* {PlotRange -> {{0, 10}, {-0.999999, 1.}}} *)
Have fun!
FullRange
withcompletePlotRange
then I just get error messages. $\endgroup$Charting`get2DPlotRange@Plot[Sin[x], {x, 0, 10}]
$\endgroup$completePlotRange
(from the top of my answer in the linked thread) with the test examples at the end of your answer using Mathematica 10.3 and it works like a charm producing output similar but only approximately equal to your's without any error messages. Please try it again with fresh kernel. $\endgroup$