matplotlib 使用gridspec时子图之间的空间太大

mcvgt66p  于 2023-04-07  发布在  其他
关注(0)|答案(2)|浏览(134)

我试图用左边12个图和右边一个更大的图组成一个网格。问题是matplotlib在小的子图之间添加了太多的空间。我如何才能摆脱这些空间?

import matplotlib.pyplot as plt

fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(4, 5)

ax0 = fig.add_subplot(gs[0, 0])
ax0.set_ylabel(r'$a_t$')
ax0.tick_params(labelbottom=False)
ax0.set_title(r'$\beta = 0$')
ax1 = fig.add_subplot(gs[1, 0], sharex=ax0)
ax1.set_ylabel(r'$x_t$')
ax1.tick_params(labelbottom=False)

ax = fig.add_subplot(gs[0, 1], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 0.5$')
ax = fig.add_subplot(gs[1, 1], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False, labelbottom=False)

ax = fig.add_subplot(gs[0, 2], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 1$')
ax = fig.add_subplot(gs[1, 2], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False, labelbottom=False)

ax = fig.add_subplot(gs[2, 0], sharex=ax0, sharey=ax0)
ax.set_ylabel(r'$a_t$')
ax.tick_params(labelbottom=False)
ax.set_title(r'$\beta = 1.5$')
ax = fig.add_subplot(gs[3, 0], sharex=ax0, sharey=ax1)
ax.set_ylabel(r'$x_t$')

ax = fig.add_subplot(gs[2, 1], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 2$')
ax = fig.add_subplot(gs[3, 1], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False)

ax = fig.add_subplot(gs[2, 2], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 3$')
ax = fig.add_subplot(gs[3, 2], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False)

ax = fig.add_subplot(gs[:, 3:])

它看起来是这样的:

下面是没有最后一行(ax = fig.add_subplot(gs[:, 3:]))的情况:

情节更加紧密,这就是我想要的效果,当然要有右边的大情节。

2admgd59

2admgd591#

如果你想尝试子图,类似于

fig0 = plt.figure(constrained_layout=True)

sfigs = fig0.subfigures(1, 2, width_ratios=[3, 2])

fig = sfigs[0]
gs = fig.add_gridspec(4, 3)
...
# code as before
...
ax = sfigs[1].subplots()

工作正常,但注意顶部脊柱不排队,因为没有标题的右手子情节

xjreopfe

xjreopfe2#

Nested Gridspecs似乎是一个很好的解决方案,其结果看起来与Jody Klymak的子图解决方案非常相似;我不确定是否有理由更喜欢其中一个。

代码:

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(constrained_layout=True, figsize=(6, 3))
gs0 = fig.add_gridspec(1, 2, width_ratios=(3, 2))
gs = gridspec.GridSpecFromSubplotSpec(4, 3, subplot_spec=gs0[0])

ax0 = fig.add_subplot(gs[0, 0])
ax0.set_ylabel(r'$a_t$')
ax0.tick_params(labelbottom=False)
ax0.set_title(r'$\beta = 0$')
ax1 = fig.add_subplot(gs[1, 0], sharex=ax0)
ax1.set_ylabel(r'$x_t$')
ax1.tick_params(labelbottom=False)

ax = fig.add_subplot(gs[0, 1], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 0.5$')
ax = fig.add_subplot(gs[1, 1], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False, labelbottom=False)

ax = fig.add_subplot(gs[0, 2], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 1$')
ax = fig.add_subplot(gs[1, 2], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False, labelbottom=False)

ax = fig.add_subplot(gs[2, 0], sharex=ax0, sharey=ax0)
ax.set_ylabel(r'$a_t$')
ax.tick_params(labelbottom=False)
ax.set_title(r'$\beta = 1.5$')
ax = fig.add_subplot(gs[3, 0], sharex=ax0, sharey=ax1)
ax.set_ylabel(r'$x_t$')

ax = fig.add_subplot(gs[2, 1], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 2$')
ax = fig.add_subplot(gs[3, 1], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False)

ax = fig.add_subplot(gs[2, 2], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 3$')
ax = fig.add_subplot(gs[3, 2], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False)

gs = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=gs0[1])
ax = fig.add_subplot(gs[0, 0])

相关问题