1

When creating axes in a figure, the bounding box (bbox) is changed at some point during plotting. I'd like to have access to the final bbox values (e.g., through ax.get_position()) while running a non-interactive script. How can I force the bbox values to update without triggering a plot-to-screen (e.g., using pl.pause)?

A minimum example is below, showing that the bbox is updated if a sequence of commands is called interactively from the ipython command line, but the bbox is not updated if used in a script (in this case, %pasted):

In [1]: import pylab as pl

In [2]: fig = pl.figure()

In [3]: ax = fig.gca()

In [4]: print(ax.get_position())
Bbox(x0=0.125, y0=0.10999999999999999, x1=0.9, y1=0.88)

In [5]:

In [5]: im = ax.imshow([[0,1],[1,0]])

In [6]: print(ax.get_position())
Bbox(x0=0.22375000000000006, y0=0.10999999999999999, x1=0.80125, y1=0.88)

In [7]:

In [7]: cb = fig.colorbar(mappable=im)

In [8]: print(ax.get_position())
Bbox(x0=0.1675000000000002, y0=0.10999999999999999, x1=0.7450000000000001, y1=0.88)

In [9]: %paste
import pylab as pl
fig = pl.figure()
ax = fig.gca()
print(ax.get_position())

im = ax.imshow([[0,1],[1,0]])
print(ax.get_position())

cb = fig.colorbar(mappable=im)
print(ax.get_position())
## -- End pasted text --
Bbox(x0=0.125, y0=0.10999999999999999, x1=0.9, y1=0.88)
Bbox(x0=0.125, y0=0.10999999999999999, x1=0.9, y1=0.88)
Bbox(x0=0.125, y0=0.10999999999999999, x1=0.7450000000000001, y1=0.88)

In [10]: print(ax.get_position())
Bbox(x0=0.1675000000000002, y0=0.10999999999999999, x1=0.7450000000000001, y1=0.88)

My main use case is setting a colorbar's location without using the axes_grid toolkit, since that changes the size of the parent axis too.

keflavich
  • 18,278
  • 20
  • 86
  • 118

0 Answers0