Function Repository Resource:

GraphicsBounds

Source Notebook

Get the plot range used in a piece of graphics

Contributed by: Taliesin Beynon

ResourceFunction["GraphicsBounds"][graphics]

gives a list {{left,right},{bottom,top}} of the coordinates that bound all graphics primitives occurring in graphics.

Details and Options

ResourceFunction["GraphicsBounds"] works only with 2D graphics.

Examples

Basic Examples (2) 

Display some graphics primitives:

In[1]:=
Graphics[{Red, Disk[{2, 2}], Green, Rectangle[{0, 0}, {2, 2}], Blue, Polygon[CirclePoints[7]]}]
Out[1]=

Get the plot range:

In[2]:=
ResourceFunction["GraphicsBounds"][%]
Out[2]=

Without displaying graphics:

In[3]:=
ResourceFunction["GraphicsBounds"][
 Graphics[{Red, Disk[{2, 2}], Green, Rectangle[{0, 0}, {2, 2}], Blue, Polygon[CirclePoints[7]]}, PlotRangePadding -> 0.6]]
Out[3]=

Get a plot:

In[4]:=
Plot[Evaluate[BesselJ[1, x]], {x, 0, 10}, Filling -> Axis]
Out[4]=

Get the plot range:

In[5]:=
ResourceFunction["GraphicsBounds"][%]
Out[5]=

Properties and Relations (2) 

Use GraphicsBounds to get the plot range of a plot:

In[6]:=
ResourceFunction["GraphicsBounds"][
 Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, Filling -> Axis]]
Out[6]=

You can get the same information using AbsoluteOptions:

In[7]:=
AbsoluteOptions[
 Plot[Evaluate[Table[BesselJ[n, x], {n, 4}]], {x, 0, 10}, Filling -> Axis], PlotRange]
Out[7]=

Possible Issues (1) 

GraphicsBounds works only with 2D graphics:

In[8]:=
ResourceFunction["GraphicsBounds"][
 Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -2, 2}]]
Out[8]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 29 January 2019

License Information