'How can I maintain exact subplot size, height ratios and hspace with varying number of subplots i Matplotlib?

There are several closely related questions asked here, however, I am unable to find a solution that works in my case.

What I am trying to achieve, is to make a subplot of n subplots. The bottom subplot will be my main subplot, and all other (n-1) subplots will be placed above with 1/10th the height. So far, so good, I can use fig, axes = plt.subplots(nrows=n, gridspec_kw={'height_ratios': [1 for i in range(n-1)]+[10]}) to generate this.

However, no matter the value of n, I want to maintain the aspect ratio of the bottom subplot, and this should be given by the width of the Figure-object. For example, if the figure width is 10 inches, rcParams['figure.subplot.right'] = 0.9 and rcParams['figure.subplot.left'] = 0.1 and the desired aspect ratio is 3:1, then I want the bottom subplot to be exactly 0.8 inches wide and 0.2666 inches high, even if I have 1, 2, 3 or 4 subplots. At the same time, the space separating the subplots should also be constant, so that if I put several of these figures side-by-side, every subplot should match up exactly, and whichever figure has the highest number of subplots would just have the rest stacked on top of that again.

I don't have a need to control the figure height, so this is the parameter I want matplotlib to automatically adjust to make this happen, but I'm really struggling to find a way to do this.

The solution in this one is close: Matplotlib - Maintain consistent subplot size for different layouts

It maintains the subplot size, but the spacing between the subplots differ with the number of subplots. There might be a way to modify that code to make it suit my purpose, but I can't decipher what changes I would have to make.

Any help here is greatly appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source