使用 matplotlib.gridspec 时子图之间的 space 过多
Too much space between subplots when using matplotlib.gridspec
我正在尝试制作一个图形,左侧有 12 个地块,右侧有一个较大的地块。问题是 matplotlib 在小子图之间添加了太多 space。我怎样才能摆脱这个 space?
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:])
):
情节更加紧密。这就是我想要的样子,当然还有右边的大图。
谢谢!
如果你想尝试子图,比如
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()
工作正常,但请注意顶部书脊没有对齐,因为右侧子图中没有标题
一个好的解决方案似乎是 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])
我正在尝试制作一个图形,左侧有 12 个地块,右侧有一个较大的地块。问题是 matplotlib 在小子图之间添加了太多 space。我怎样才能摆脱这个 space?
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:])
):
情节更加紧密。这就是我想要的样子,当然还有右边的大图。
谢谢!
如果你想尝试子图,比如
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()
工作正常,但请注意顶部书脊没有对齐,因为右侧子图中没有标题
一个好的解决方案似乎是 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])