控制Matplotlib子图的空白间隔

7
我想知道:我有一个1行4列的图表。然而,前三个子图共享相同的y轴范围(即它们具有相同的范围并代表相同的内容)。第四张不是这样的。
我想做的是改变前三个图的wspace,使它们紧密相连(并成组),然后第四个图留出一些空间,没有y轴标签等重叠。
我可以通过一点photoshop编辑来简单地完成这个过程...但我想有一个编码版本。我该怎么做?
1个回答

12
您最可能需要的是 GridSpec。它使您能够自由调整子图组的 wspace
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np

fig = plt.figure()
# create a 1-row 3-column container as the left container
gs_left = gridspec.GridSpec(1, 3)

# create a 1-row 1-column grid as the right container
gs_right = gridspec.GridSpec(1, 1)

# add plots to the nested structure
ax1 = fig.add_subplot(gs_left[0,0])
ax2 = fig.add_subplot(gs_left[0,1])
ax3 = fig.add_subplot(gs_left[0,2])

# create a 
ax4 = fig.add_subplot(gs_right[0,0])

# now the plots are on top of each other, we'll have to adjust their edges so that they won't overlap
gs_left.update(right=0.65)
gs_right.update(left=0.7)

# also, we want to get rid of the horizontal spacing in the left gridspec
gs_left.update(wspace=0)

现在我们得到了如下结果:

enter image description here

当然,你可能想要对标签等进行一些操作,但现在你已经可以调整间距了。 GridSpec 可以用于生成一些相当复杂的布局。请查看: http://matplotlib.org/users/gridspec.html

绝对完美的答案,集成无误。非常感谢DrV!! - user3125347

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接