Actually, what you’re wanting is quite simple… You just need to make sure that adjustable
is set to 'box'
on your axes, and you have a set aspect ratio for the axes (anything other than 'auto'
).
You can either do this with the adjustable
kwarg when you create the subplots. Alternatively, you can do this after their creation by calling ax.set_adjustable('box')
, or by calling ax.set_aspect(aspect, adjustable="box")
(where aspect is either 'equal'
or a number).
Now, regardless of how the figure is resized, the subplots will maintain the same aspect ratio.
For example:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(2,1,1, adjustable="box", aspect=0.3)
ax2 = fig.add_subplot(2,1,2)
ax1.plot(range(10))
ax2.plot(range(10))
plt.show()
Now, compare how the top subplot responds to resizing, vs. how the bottom subplot responds:
The initial plot
Resized to a vertical layout:
Resized to a horizontal layout: