控制matplotlib子批次的wspace

2024-10-01 11:30:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在想:我有一个1 row, 4 column图。然而,前三个子块共享相同的yaxes范围(即它们具有相同的范围并表示相同的事物)。第四个没有。在

我想做的是改变第一个三个图的wspace使它们相接触(并被分组),然后第四个图则是间隔一点,不重叠yaxis标签,等等

我可以简单地通过一点photoshop编辑来实现这一点……但我希望有一个编码版本。我怎么能这么做?在


Tags: 版本编辑编码间隔column标签事物row
1条回答
网友
1楼 · 发布于 2024-10-01 11:30:24

您可能最想要的是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

相关问题 更多 >