Have a look at the
adjustableparameter of the axes. This controls whether
the data limits or the bounding rectangle’s shape are changed when the
aspect/limits are changed.
Normally, the options are
"box"and
"datalim", but there’s a special case
for shared axes. In your case, you want
adjustable='box-forced'.
As a quick example (I took the liberty of simplifying things slightly, feel
free to use the separate
set_foomethods if they’re clearer to you. If you
take that route, the method is
ax.set_adjustable.):
import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0,1.6,50) + 50.0fig, ax = plt.subplots()ax2 = ax.twinx()XLIM = [50.0, 51.6]YLIM = [0.0, 1.1, 0.0, 11.0]ax.plot(x,np.sin(x-50.0),'b')ax2.plot(x,np.cos(x-50.0)*10.,'r')ax.set(adjustable='box-forced', xlim=XLIM, ylim=YLIM[:2], xticks=np.arange(XLIM[0], XLIM[1], 0.2), yticks=np.arange(YLIM[0], YLIM[1]+0.1, 0.1)[:-1], aspect=(XLIM[1]-XLIM[0])/(YLIM[1]-YLIM[0]))ax2.set(adjustable='box-forced', ylim=YLIM[2:], yticks=np.arange(YLIM[2], YLIM[3]+1.0, 1.0), aspect=(XLIM[1]-XLIM[0])/(YLIM[3]-YLIM[2]))ax.grid(True, which='major',linestyle='solid')plt.show()



