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.
Asked
Active
Viewed 116 times
2
2 Answers
1
Try this:
Select[(Plot[Sin[x], {x, 0, 10}] // Options), #[[1]] == PlotRange &]
(* {PlotRange -> {{0, 10}, {-0.999999, 1.}}} *)
Have fun!
Alexei Boulbitch
- 39,397
- 2
- 47
- 96
1
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
Jason B.
- 68,381
- 3
- 139
- 286
FullRangewithcompletePlotRangethen I just get error messages. – Jason B. Nov 30 '15 at 11:46Charting`get2DPlotRange@Plot[Sin[x], {x, 0, 10}]– Kuba Nov 30 '15 at 11:50completePlotRange(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. – Alexey Popkov Nov 30 '15 at 13:45completePlotRange/@{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}}]}returns{{{-0.208333,10.2083},{-1.11111,1.11111}},{{-0.208333,10.2083},{-2.,2.}},{{-0.208333,10.2083},{-1.11111,1.11111}},{{-20.322,2200.},{-2.,2.}}}. – Alexey Popkov Nov 30 '15 at 13:56Quitbefore saying something, my mistake. – Jason B. Nov 30 '15 at 14:15