I have 2 subplots the first of which has a fixed ratio. The lower subplot I don't care about the ratio but the width should align with the upper subplot.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(211, aspect='equal', autoscale_on=False, xlim=(0, 80), ylim=(0, 40))
ax.plot([0,10,20,40,60,70], [1,4,3,2,6,5], 'bo')
ax1 = fig.add_subplot(212, xlim=(0, 8000), ylim=(-200, 0))
ax1.plot([0,8000],[-200,0], '-')
plt.show()
How can I make both subplots have the same width?
Update:
I made it work!
import matplotlib.pyplot as plt
import numpy as np
gkw = {'height_ratios':[1, 2] }
fig, (ax1, ax2) = plt.subplots(2, 1, gridspec_kw = gkw )
ax1.set_aspect('equal')
ax1.set_autoscale_on(False)
ax1.set_xlim(left=0, right=80)
ax1.set_ylim(bottom=0, top=40)
ax1.plot([0,10,20,40,60,70], [1,4,3,2,6,5], 'bo')
ax2.set_xlim(left=0, right=8000)
ax2.set_ylim(bottom=-200, top=0)
ya = np.diff(np.array(ax2.get_ylim()))[0]
xa = np.diff(np.array(ax2.get_xlim()))[0]
wa = gkw['height_ratios'][0]/float(gkw['height_ratios'][1])
ia = 40/80
ax2.set_aspect(float(1/wa*ia/(ya/xa)))
ax2.plot([0,8000],[-200,0], '-')
plt.show()
