我目前正在尝试使用 GridSpec 在 Matplotlib (Python 3.6、Matplotlib 2.0.0)中绘制许多子图。这是最小的工作示例:
import matplotlib.pyplot as plt
from matplotlib.gridspec import *
# Color vector for scatter plot points
preds = np.random.randint(2, size=100000)
# Setup the scatter plots
fig = plt.figure(figsize=(8,8))
grid = GridSpec(9, 9)
# Create the scatter plots
for ii in np.arange(0, 9):
for jj in np.arange(0, 9):
if (ii > jj):
ax = fig.add_subplot(grid[ii, jj])
x = np.random.rand(100000)*2000
y = np.random.rand(100000)*2000
ax.scatter(x, y, c=preds)
这是没有任何修改的结果:
当然,子图之间的间距并不令人满意,所以我做了我通常做的事情并使用tight_layout()
。但如下图所示,tight_layout()
不可接受地挤压图的宽度:
而不是使用tight_layout()
,我想我应该使用手动调整子图subplots_adjust()
。下图是带有subplots_adjust(hspace=1.0, wspace=1.0)
.
结果几乎是正确的,如果稍微调整一下子图之间的空间就会很完美。然而,子图看起来太小,无法充分传达信息。
有没有更好的方法来获得子图之间的适当间距,同时仍然保持纵横比和足够大的子图尺寸?我能想到的唯一可能的解决方案是使用subplots_adjust()
具有较大的figsize
,但这会导致图形边缘和子图之间存在非常大的空间。
任何解决方案表示赞赏。