6
$\begingroup$

Say I have some plot

plot = Plot[Sin[x], {x,0,10}] (*as an example*)

Is there a way I can get the x and y axes min/max values from the "plot" variable?

If I wanted the image sizes I could do

ImageDimensions[plot]

I'm looking for something like this but for the axes (either total length or min/max values). I want to draw some additional graphics on the plot but it needs to be appropriately scaled.

Thanks!

$\endgroup$
  • $\begingroup$ I can't give a complete answer from mobile, but you will want to check out the Scaled function, plus my GetGeometry function in my recent question "Context dependent functions" $\endgroup$ – LLlAMnYP Mar 26 '15 at 18:52
  • $\begingroup$ are you actually looking for the coordinate range on the plot or are you thinking in terms of the image? The plots Plot[10^6 Sin[x],{x,0,10}] and Plot[Sin[x],{x,0,10}] look very similar, you could place them on top of each other, however the y-range of one is +-10^6, while the other is +-1. Both plots would be 600 points wide and 600/GoldenRatio high. Axes dimensions in printer points would be similar. $\endgroup$ – LLlAMnYP Mar 26 '15 at 19:01
  • $\begingroup$ Strongly related: "How to get the real PlotRange?" $\endgroup$ – Alexey Popkov Mar 26 '15 at 23:25
16
$\begingroup$

Also

PlotRange[plot]
PlotRange /. AbsoluteOptions[plot]
Last @@ AbsoluteOptions[plot, PlotRange]
PlotRange /. plot[[2]]

all give

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

Note: Regarding usage of PlotRange as a function, it is undocumented, and the earliest reference I could find on this site is this answer dated Oct 11, 2012:

Since then, also used in

$\endgroup$
  • 1
    $\begingroup$ +1 PlotRange[plot] is a very interesting answer - an option rule that can behave as a function. I didn't see anything in the documentation that PlotRange (or other options, for that matter) could behave in this fashion. $\endgroup$ – bobthechemist Mar 26 '15 at 21:16
  • $\begingroup$ @bob, thank you for the vote. It is not documented afaik. My initial discovery was pure accident. I used it in several answers on this site but i cannot find any of them when i search now. $\endgroup$ – kglr Mar 26 '15 at 21:24
  • $\begingroup$ Mr.Wizard could not recall recently where he learnt that PlotRange trick. Now I know it was probably from you and that it is indeed not documented ... interesting ;) $\endgroup$ – SquareOne Mar 26 '15 at 21:43
  • $\begingroup$ @SquareOne, still searching earlier posts - no success so far. $\endgroup$ – kglr Mar 26 '15 at 21:48
  • $\begingroup$ This is how to you can make the additional functionality of the Options visible: ClearAttributes[Evaluate[Options[Graphics]〚All, ,1〛], ReadProtected] Information /@ Options[Graphics]〚All, 1〛 unfortunately the result is that PlotRange is the only option of Graphics that is also a Function. Its Definition is: PlotRange[- GraphicsArray-]:=With[{SystemDumpres=Graphics[-GraphicsArray-]},PlotRange[SystemDumpres]/;SystemDumpGRTest[SystemDumpres]] and something similar with -RasterGraphics-. Really interesting, maybe there are more options like this to find! $\endgroup$ – sacratus Mar 27 '15 at 20:24
3
$\begingroup$

FilterRules[AbsoluteOptions[plot], PlotRange] does the trick

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

Not sure if this is an exhaustive answer.

$\endgroup$
  • $\begingroup$ Uh... yeah, obvious solution is obvious. My thinking gets boxy after 7 hours at Schiphol. The harder part is getting the absolute (or printer, or whatever you wish to call them) coordinates, at which I hinted in my comment. $\endgroup$ – LLlAMnYP Mar 26 '15 at 18:57
3
$\begingroup$

Anyway, while I wait for my flight, here's some code that'll give you everything there is to know about a plot.

GetGeometry[g_Graphics] :=
    Module[{
        q,
        dim,
        plotrange=PlotRange/.AbsoluteOptions[g,PlotRange],
        },

        q=Rasterize[Show[g,
                    Epilog->{Annotation[Rectangle[ImageScaled[{0,0}],ImageScaled[{1,1}]],"One","Region"],
                            Annotation[Rectangle[Scaled[{0,0}],Scaled[{1,1}]],"Two","Region"]}],"Regions"][[-2;;-1,2]];

        s=q[[1,2]]-q[[1,1]];
        q=q[[2]];
        dim=If[Norm[s-ImageDimensions[g]]<Sqrt[2],s,ImageDimensions[g]];

        {
        "PlotRange"->plotrange,
        "ImageSize"->dim,
        "PlotRangeSize"->q[[2]]-q[[1]],
        "ImagePadding"->{{q[[1,1]],dim[[1]]-q[[2,1]]},{dim[[2]]-q[[2,2]],q[[1,2]]}},
        "AspectRatio"->(q[[2,2]]-q[[1,2]])/(q[[2,1]]-q[[1,1]]),
        "ImageScaledToScaled"->(({{-q[[1,1]],-dim[[2]]+q[[2,2]]},{dim[[1]]-q[[2,1]],q[[1,2]]}})/(q[[2]]-q[[1]]))+{{0,0},{1,1}}
        }
    ]

Edit

The code above had some excessive definitions which I removed (the full version of my function calculates the amount of padding necessary for the ticks and frame labels).

Most of the output of the function is self-explanatory, but "PlotRangeSize" gives the size of the PlotRange in printer points and "ImageScaledToScaled" gives the coordinates of Scaled[{0,0}] and Scaled[{1,1}] in terms of ImageScaled.

$\endgroup$
0
$\begingroup$
Cases[Plot[Sin[x], {x, 0, 10}], _[PlotRange, x_] :> x, -1][[1]]
(*{{0, 10}, {-0.999999, 1.}})*
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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