0

How can I extract the used plot range from a plot?

I create a plot:

plot = Plot[Sin[x], {x, 0, 2 \[Pi]}, Frame -> True, 
  PlotRangePadding -> {0, Scaled[.2]}]

When I try to extract the plot range with: AbsoluteOptions or PlotRange, I incorrectly get: {{0., 6.28319}, {-1., 1.}} This misses the plot range padding.

Manually adding it

{px0, py0} = Quiet[AbsoluteOptions[plot, PlotRange]][[1, 2]];
{xpad, ypad} = 
  Map[Identity @@ # &, 
   Quiet[AbsoluteOptions[plot, PlotRangePadding]][[1, 2]], {2}];
{lx, ly} = Differences[#][[1]] & /@ {px0, py0};
{px, py} = 
 MapThread[{#1[[1]] - #3[[1]] #2, #1[[2]] + #3[[2]] #2} &, {{px0, 
    py0}, {lx, ly}, {xpad, ypad}}]

does not lead to the correct answer either. I get {{0., 6.28319}, {-1.4, 1.4}} instead of {{0., 6.28319}, {-1.666667, 1.666667}}.

Oscillon
  • 1,231
  • 10
  • 21
  • @CarlWoll Yes, your GraphicsInformation paclet is one solution. However, with Mma 12 just released, is there still no simpler solution available? I did not find your solution before due to the misleading title of the other question. – Oscillon Apr 23 '19 at 20:06
  • The source code of the paclet is explained in this question: https://mathematica.stackexchange.com/questions/2091/retrieving-the-imagepadding-in-absolute-units/138907#a – Oscillon Apr 24 '19 at 07:24

1 Answers1

0

Just do this:

Options[plot, PlotRangePadding]

{PlotRangePadding -> {{0, 0}, {Scaled[0.2], Scaled[0.2]}}}

or

enter image description here

{PlotRange -> {{0, 2 [Pi]}, {-1., 1.}}}

David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • Unfortunately this does not return the correct used y plot range. It should be {-1.666667, 1.666667}, not {-1., 1.}. – Oscillon Apr 24 '19 at 07:08