如何通过调整之间的空间来对子图进行分组
How to group subplots by adjusting spaces in between
我有一个子图如下所示:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
fig_shape, axs_shape = plt.subplots(2, 6, figsize=(6, 6))
for i in range(2):
for j in range(6):
axs_shape[i, j].xaxis.set_major_locator(plt.NullLocator())
axs_shape[i, j].yaxis.set_major_locator(plt.NullLocator())
for i in range(6):
axs_shape[int(i / 3), 2 * (i % 3)].plot(x, y)
axs_shape[int(i / 3), 2 * (i % 3) + 1].plot(x, y)
我想要的是,子图以两个为一组。这意味着,在每一行中,我希望绘图 0 和 1 彼此相邻(中间没有 space)。然后是一个小 space,紧接着是相邻的地块 2 和地块 3。然后 space 并紧挨着绘制 4 和 5。我读到,您可以使用 .tight_layout()
和 subplots_adjust
调整大小,但我无法找到针对此特定行为的解决方案。非常感谢您的帮助!
您可以使用嵌套的网格规格:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
x = [1, 2, 3]
y = [4, 5, 6]
fig = plt.figure(figsize=(12, 5))
outer = gridspec.GridSpec(nrows=2, ncols=3)
axs = []
for row in range(2):
for col in range(3):
inner = gridspec.GridSpecFromSubplotSpec(nrows=1, ncols=2, subplot_spec=outer[row, col], wspace=0)
axs += [plt.subplot(cell) for cell in inner]
for ax in axs:
ax.plot(x, y)
ax.set_yticks([])
ax.set_xticks([])
plt.tight_layout()
plt.show()
PS:正如另一个答案中提到的,matplotlib 已将 subfigures
作为一项新功能实现。如果我没理解错的话,上面的例子大概是这样的:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
fig = plt.figure(figsize=(12, 5), constrained_layout=True)
subfigs = fig.subfigures(nrows=2, ncols=3, wspace=0.07)
axs = [subfig.subplots(nrows=1, ncols=2, gridspec_kw={'wspace': 0}) for subfig in subfigs.ravel()]
for subax in axs:
for ax in subax:
ax.plot(x, y)
ax.set_yticks([])
ax.set_xticks([])
plt.show()
使用当前的matplotlib 3.4.1,我似乎无法无间隙地绘制内部图。设置 constrained_layout=False
甚至会使最右边的 4 个子图消失。现在看起来像:
这是新子图功能的目标:https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subfigures.html?highlight=subfigure
我有一个子图如下所示:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
fig_shape, axs_shape = plt.subplots(2, 6, figsize=(6, 6))
for i in range(2):
for j in range(6):
axs_shape[i, j].xaxis.set_major_locator(plt.NullLocator())
axs_shape[i, j].yaxis.set_major_locator(plt.NullLocator())
for i in range(6):
axs_shape[int(i / 3), 2 * (i % 3)].plot(x, y)
axs_shape[int(i / 3), 2 * (i % 3) + 1].plot(x, y)
我想要的是,子图以两个为一组。这意味着,在每一行中,我希望绘图 0 和 1 彼此相邻(中间没有 space)。然后是一个小 space,紧接着是相邻的地块 2 和地块 3。然后 space 并紧挨着绘制 4 和 5。我读到,您可以使用 .tight_layout()
和 subplots_adjust
调整大小,但我无法找到针对此特定行为的解决方案。非常感谢您的帮助!
您可以使用嵌套的网格规格:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
x = [1, 2, 3]
y = [4, 5, 6]
fig = plt.figure(figsize=(12, 5))
outer = gridspec.GridSpec(nrows=2, ncols=3)
axs = []
for row in range(2):
for col in range(3):
inner = gridspec.GridSpecFromSubplotSpec(nrows=1, ncols=2, subplot_spec=outer[row, col], wspace=0)
axs += [plt.subplot(cell) for cell in inner]
for ax in axs:
ax.plot(x, y)
ax.set_yticks([])
ax.set_xticks([])
plt.tight_layout()
plt.show()
PS:正如另一个答案中提到的,matplotlib 已将 subfigures
作为一项新功能实现。如果我没理解错的话,上面的例子大概是这样的:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
fig = plt.figure(figsize=(12, 5), constrained_layout=True)
subfigs = fig.subfigures(nrows=2, ncols=3, wspace=0.07)
axs = [subfig.subplots(nrows=1, ncols=2, gridspec_kw={'wspace': 0}) for subfig in subfigs.ravel()]
for subax in axs:
for ax in subax:
ax.plot(x, y)
ax.set_yticks([])
ax.set_xticks([])
plt.show()
使用当前的matplotlib 3.4.1,我似乎无法无间隙地绘制内部图。设置 constrained_layout=False
甚至会使最右边的 4 个子图消失。现在看起来像:
这是新子图功能的目标:https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subfigures.html?highlight=subfigure