Divider.append_axes kills ability to retrieve axes boundaries?

What I want is to have two axes grids, one rectangular and one polar, covering the same square axes.
The way I do that is:

fig1,ax = plt.subplots()
(this draws the rectangular axes)
rect = ax.get_position().bounds
ax_polar = fig1.add_axes(rect,polar=True,frameon=True)
(this draws the polar axes)

However, I also need to add a colorbar to this plot. One way to precisely locate the colorbar is to use:
cax = divider.append_axes("right",size="5%",pad=0.2)
cb = fig1.colorbar(im,cax=cax,...)

But when I do this, the rectangular axes resize and the polar axes go out of sync. If I try to retrieve the ax boundaries as above, I get the same values–presumably because ax has been subdivided, so it together with cax covers the same extent–which is not what I need. In other words, after the resizing because of divider, the bounds of ax do not get updated.

Is there a solution? Or an easier way to create the two axes and have them permanently tied together?

Can you give us a copy-paste-runnable example of what you mean? I can think of a couple ways to implement what you are describing.

My knee-jerk guesses are either that there is some aspect ratio related issues or some ordering or race-condition type issues.

Sure:

x = np.linspace(-150,150,30)
y = np.linspace(-150,150,30)
X,Y = np.meshgrid(x,y)
Z = np.random.rand(30,30)

fig1,ax = plt.subplots(figsize=(10,10))
im = ax.imshow(Z,extent=[-150,150,-150,150],alpha=0.3)
ax.set_xlim(-150,150)
ax.set_ylim(-150,150)
rect = ax.get_position().bounds

ax_polar = fig1.add_axes(rect,polar=True, frameon=True, xticks=([]), yticks=([]))
ax_polar.patch.set_alpha(0)
ax_polar.tick_params(labelbottom=False,labelleft=False)
ax_polar.set(yticks=np.linspace(0,150,4))
ax_polar.set(xticks=np.linspace(0,2*np.pi,13))
ck = [ax_polar.get_rmax(),ax_polar.get_rmax()*0.98]
for t  in np.deg2rad(np.arange(0,360,10)):
    ax_polar.plot([t,t], tick, lw=0.72, color='k')
ax_polar.set_ylim(0,150)

# Remove comments from below lines
from mpl_toolkits.axes_grid1.axes_divider import   make_axes_locatable
#divider = make_axes_locatable(ax)
#cax = divider.append_axes("right", size="5%", pad=0.2)
#cb = fig1.colorbar(im,cax=cax,shrink=1.0)

Run it first like this, then remove the comments in the last three lines.

Also, I found out that if I don’t do the imshow command, then the two set of axes do not overlap perfectly in the x direction.

Sorry for the very short post (hopefully will follow up with more details later).

ax_polar.set_position(ax.get_position())

will match the position of the polar axes to what ever the position of the image axes is (but you have have to force a draw before the position of the image axes is correct).

If you set the aspect (which imshow does by default), we need to adjust either the data limits or the axes size (and position) to get the requested aspect ratio.

I do not know of a clean way off the top of my head to keep the position of two axes in sync automatically.