Figure points vs. figure pixels

I have some figures with multiple axes stacked on top of each other, generated with Figure.add_subplot(). In each figure, some sets of these axes are logically grouped together, and I need some visual clue of which axes are more closely related.

The right way is probably to use GridSpec [] and add some additional wspace.

Instead, I attempted to just add some lines between the logical groups of axes (code below), generated from the bounding boxes of the axes, and drawing the lines halfway between.

There are probably many things wrong with this code. :slight_smile: If I use ‘figure pixels’ (red lines), the displayed figure looks great, but the lines move to different places in the saved PNG file. I’m guessing this is the result of rcParams.update({‘savefig.dpi’: 300.0}). If I use ‘figure points’ (green lines), the lines show up in the same place both in the display and saved file. Is there a different function I should be using to get the bounding box edges, or to convert between the figure pixels and points?




f = figure()

map(lambda i : f.add_subplot(10, 1, i), range(1,10)) # make subplots

do_lines_after = [1,3,5,7] # draw lines after these axes (indexes)

def group_axes(f, do_lines_after, color=‘red’, coords=‘figure pixels’):

boxes = [ax.bbox.get_points() for ax in f.axes] # get bounding boxes

for ax_index in do_lines_after:

if ax_index < (len(boxes) - 1):

(x0, y1), (x1, _) = boxes[ax_index] # y1 -> bottom of upper axes

y0 = boxes[ax_index+1][1][1] # y0 -> top of lower axes

y = y0 + (y1 - y0)/2.0 # halfway in between the two axes

dict(arrowstyle=’-’, facecolor=color, edgecolor=color)

ax.annotate(’’, xy=(x00.3, y), xytext=(x11.01, y),

arrowprops=arrowprops, xycoords=coords, textcoords=coords)


group_axes(f, do_lines_after, ‘red’, ‘figure pixels’)

group_axes(f, do_lines_after, ‘green’, ‘figure points’)

f.canvas.draw() # pixels/red look good, green notsomuch

f.savefig(‘test.png’) # green in the same place; red is somewhere else now (based on dpi?)