4
$\begingroup$

Is there a function that gives you dimensions of Graphics? I mean x and y dimensions of rectangular region that the content of Graphics would fit into.

Graphics[{Table[
   Circle[RandomReal[{-10, 10}, 2], RandomReal[{0, 10}, 2], 
    RandomReal[{0, 2 π}, 2]], {n, 100}]}]

enter image description here

Update:

PlotRange is of no use since it is far from being precise.

v = Table[
   Circle[RandomReal[{-10, 10}, 2], RandomReal[{0, 10}, 2], 
    RandomReal[{0, 2 π}, 2]], {n, 100}];
Graphics[{v}];
Graphics[{v, {Transparent, EdgeForm[Red], 
   Rectangle @@ Transpose[PlotRange[%]]}}]

enter image description here

Also BoundingRegionis of no use for the same reason - being imprecise.

$\endgroup$
2
  • $\begingroup$ It does not work for any Graphics. It is not that it does not work only in specific situations. $\endgroup$ – azerbajdzan Oct 13 '20 at 9:19
  • $\begingroup$ You could e.g. try: Rasteriz[ graphics , "RasterSize"] $\endgroup$ – Daniel Huber Oct 13 '20 at 10:06
6
$\begingroup$

You can use the ResourceFunction "GraphicsInformation" for this purpose:

SeedRandom[1];
g = Graphics[{
    Table[
        Circle[RandomReal[{-10,10},2],RandomReal[{0,10},2],RandomReal[{0,2 π},2]],
        {n, 100}
    ]

enter image description here

Using the "GraphicsInformation" resource function:

pr = Lookup[ResourceFunction["GraphicsInformation"][g], "PlotRange"]

{{-17.8295, 17.0196}, {-19.7501, 19.9222}}

Check:

Show[
    g, 
    Epilog -> {
        EdgeForm[Red],
        FaceForm[None],
        Rectangle[{pr[[1,1]], pr[[2,1]]}, {pr[[1,2]], pr[[2,2]]}]
    }
]

enter image description here

"GraphicsInformation" also returns other pieces of useful information:

Keys @ ResourceFunction["GraphicsInformation"][g]

{"ImagePadding", "ImageSize", "PlotRangeSize", "ImagePaddingSize",
"PlotRange", "BasePlotRange"}

$\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.