1

I have to do several subplots, each with an inset at the same custom location relative to the subplot.

It can be done in principle with axes_grid1 as given in this example, however, the location parameter loc= in inset_axes or zoomed_inset_axes is not specific enough for my purpose.

I have also tried with GridSpec's get_grid_positions, but I cannot make sense of the parameters it returns.

How can I get insets to look the same in each subplot without being forced to put them at specific locations?

ali_m
  • 71,714
  • 23
  • 223
  • 298
jacob
  • 869
  • 2
  • 10
  • 23
  • This has in principle been answered [in this post](https://stackoverflow.com/questions/17458580/embedding-small-plots-inside-subplots-in-matplotlib), also more detailed [in this post](https://stackoverflow.com/questions/45378918/specific-location-for-inset-axes). There is a [full example](https://matplotlib.org/gallery/axes_grid1/inset_locator_demo.html) available in the docs as well. – ImportanceOfBeingErnest Dec 07 '18 at 10:54

1 Answers1

4

You can do it by hand:

ncols = 2
nrows = 2

inset_hfrac = .3
inset_vfrac = .3

inset_hfrac_offset = .6
inset_vfrac_offset = .6

top_pad = .1
bottom_pad = .1
left_pad = .1
right_pad = .1

hspace = .1
vspace = .1

ax_width = (1 - left_pad - right_pad - (ncols - 1) * hspace) / ncols
ax_height = (1 - top_pad - bottom_pad - (nrows - 1) * vspace) / nrows

fig = figure()

ax_lst = []
for j in range(ncols):
    for k in range(nrows):
        a_bottom = bottom_pad + k * ( ax_height + vspace)
        a_left = left_pad + j * (ax_width + hspace)

        inset_bottom = a_bottom + inset_vfrac_offset * ax_height
        inset_left = a_left + inset_hfrac_offset * ax_width

        ax = fig.add_axes([a_left, a_bottom, ax_width, ax_height])
        ax_in = fig.add_axes([inset_left, inset_bottom, ax_width * inset_hfrac, ax_height *  inset_vfrac])
        ax_lst.append((ax,ax_in))

This costs you the ability to use tight_layout, but gains you a tremendous amount of control.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • That works nicely! Do you think it's possible to relate your parameters (a_bottom, a_left) to the ones returned by gridspec's get_grid_positions(fig)? – jacob Jan 30 '13 at 11:30
  • It is probably possible, but I am not familiar with `get_grid_positions`. – tacaswell Jan 30 '13 at 13:48