擅长:python、mysql、java
<p>乔的两个选择都给了我一些问题:前者与直接使用<code>figure.tight_layout</code>而不是<code>figure.set_tight_layout()</code>有关,后者与一些后端(<em>UserWarning:tight_layout:falling back to Agg renderer</em>)有关。但乔的回答无疑为我找到了另一个紧凑的选择。这是接近OP的问题的结果:</p>
<pre><code>import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=1, sharex='col', sharey=True,
gridspec_kw={'height_ratios': [2, 1]},
figsize=(4, 7))
fig.set_tight_layout({'rect': [0, 0, 1, 0.95], 'pad': 1.5, 'h_pad': 1.5})
plt.setp(axes, title='Test')
fig.suptitle('An overall title', size=20)
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/KT8ke.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KT8ke.png" alt="enter image description here"/></a></p>