Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug report
Producing 4 subplots with a rectangular figure (height > width) and setting the subplots toax.set_aspect('equal')
causesfig.tight_layout(pad=0., h_pad=0., w_pad=0.)
to introduce ahuge vertical gap between the subplots on the first call.
Each subsequent call totight_layout
reduces the gap slowly, until the set argumentspad
andh_pad
are satisfied.
Code for reproduction
import matplotlib.pyplot as pltimport numpy as nprandarr = np.random.rand(200, 4)txtbox = 'this is some\ntext with multiple\nlines and absolutely\nno meaning'fig, ((a1, a2), (a3, a4)) = plt.subplots(2, 2, figsize=(16/2.54, 25/2.54))a1.scatter(randarr[:, 0] * 3, randarr[:, 1] * 3, label='a\nb')a2.scatter(randarr[:, 1] * 2, randarr[:, 2] * 2, label='b\nc')a3.scatter(randarr[:, 2] * 2, randarr[:, 3] * 2, label='c\nd')a4.scatter(randarr[:, 3] / 3, randarr[:, 0] / 3, label='d\ne')_ = [_ax.set_aspect('equal') for _ax in (a1, a2, a3, a4)]fig.tight_layout(pad=0., h_pad=1., w_pad=1.)
To reduce the vertical gap until padding is satisfied:
for _ in range(10): fig.tight_layout(pad=0., h_pad=1., w_pad=1.)
And it seems that for plots with a high complexity, calls tofig.canvas.draw
are required between the calls totight_layout
. But I cannot reproduce this reliably:
for _ in range(10): fig.tight_layout(pad=0., h_pad=1., w_pad=1.) fig.canvas.draw()
Actual outcome
The outcome of the code without repeated calls to tight_layout:
Expected outcome
The in my opinion correct output can be produced with:
import matplotlib.pyplot as pltimport numpy as nprandarr = np.random.rand(200, 4)txtbox = 'this is some\ntext with multiple\nlines and absolutely\nno meaning'fig, ((a1, a2), (a3, a4)) = plt.subplots(2, 2, figsize=(16/2.54, 25/2.54))a1.scatter(randarr[:, 0] * 3, randarr[:, 1] * 3, label='a\nb')a2.scatter(randarr[:, 1] * 2, randarr[:, 2] * 2, label='b\nc')a3.scatter(randarr[:, 2] * 2, randarr[:, 3] * 2, label='c\nd')a4.scatter(randarr[:, 3] / 3, randarr[:, 0] / 3, label='d\ne')_ = [_ax.set_aspect('equal') for _ax in (a1, a2, a3, a4)]for _ in range(10): fig.tight_layout(pad=0., h_pad=1., w_pad=1.)
Matplotlib version
- Operating system: Windows 10 64bit
- Matplotlib version: 3.3.1
- Matplotlib backend: Qt5Agg
- Python version: 3.7.7
- Other libraries: Numpy v 1.19.1
All packages are installed from the default conda channel.