2
$\begingroup$

This question already has an answer here:

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.

$\endgroup$

marked as duplicate by Kuba, user9660, Alexey Popkov, dr.blochwave, Jason B. Nov 30 '15 at 14:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @Kuba, I can't get the accepted answer from that post to work. If you look at the test at the end of my answer here, and replace FullRange with completePlotRange then I just get error messages. $\endgroup$ – Jason B. Nov 30 '15 at 11:46
  • $\begingroup$ @JasonB I would go with Charting`get2DPlotRange@Plot[Sin[x], {x, 0, 10}] $\endgroup$ – Kuba Nov 30 '15 at 11:50
  • $\begingroup$ That works, but I must say that having hidden functions just takes all the fun out of it. $\endgroup$ – Jason B. Nov 30 '15 at 11:56
  • $\begingroup$ @JasonB maybe but quite often they are more stable than documented :) $\endgroup$ – Kuba Nov 30 '15 at 12:19
  • $\begingroup$ @JasonB I just have checked 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$ – Alexey Popkov Nov 30 '15 at 13:45
1
$\begingroup$

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

$\endgroup$
1
$\begingroup$

Try this:

 Select[(Plot[Sin[x], {x, 0, 10}] // Options), #[[1]] == PlotRange &]

(*  {PlotRange -> {{0, 10}, {-0.999999, 1.}}}  *)

Have fun!

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.