matplotlib 如何调整gridspec间距

wtlkbnrh  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(212)

我有一个3x2子图的matplotlib图。我希望列之间没有间距(实现),第一行(a,b)和第二行(c,d)之间没有间距。但是,第三行(e,f)应该与第二行分开,因为它有不同的数据。
另外,我将子情节的标题放在第三行,以帮助读者理解内容。不幸的是,标签在中间接触。我想添加一些间距,例如不间断的空格。
我并不固定使用gridspec,我只是使用它,因为我对它有点熟悉。整个图的高度和宽度是固定的。
目前我的身材看起来是这样的:

使用以下代码生成:

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms

fig_width = 150/25.4
fig_height = 100/25.4
fig = plt.figure(figsize=(fig_width, fig_height))
gs = fig.add_gridspec(3, 2, wspace=0, hspace=0.6, height_ratios=[3,3,2])
(a, b, c) = gs.subplots(sharey='row')
b[0].set_xlabel("x")
b[1].set_xlabel("x")
c[0].set_xlabel("t")
c[1].set_xlabel("t")
trans = mtransforms.ScaledTranslation(10/72, -5/72, fig.dpi_scale_trans)
a[0].text(0.0, 1.0, "a", transform=a[0].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
a[1].text(0.0, 1.0, "b", transform=a[1].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
b[0].text(0.0, 1.0, "c", transform=b[0].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
b[1].text(0.0, 1.0, "d", transform=b[1].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
c[0].text(0.0, 1.0, "e", transform=c[0].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
c[1].text(0.0, 1.0, "f", transform=c[1].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
c[0].set_title("left", loc="left")
c[0].set_title("right", loc="right")
c[1].set_title("left", loc="left")
c[1].set_title("right", loc="right")
fig.tight_layout()
plt.show()
xjreopfe

xjreopfe1#

您可以使用subgridspec
至于标题-您可以在标题字符串后附加/前置一个空格,或者调整水平位置,如下所示。

import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms

fig_width = 150/25.4
fig_height = 100/25.4
fig = plt.figure(figsize=(fig_width, fig_height))

gs = fig.add_gridspec(2, 1, hspace=0.6, height_ratios=[6,2])

gs0 = gs[0].subgridspec(2, 2, wspace=0, hspace=0)
gs1 = gs[1].subgridspec(1, 2, wspace=0)

a, b = gs0.subplots(sharey='row', sharex='col')
c = gs1.subplots(sharey='row')

b[0].set_xlabel("x")
b[1].set_xlabel("x")
c[0].set_xlabel("t")
c[1].set_xlabel("t")
trans = mtransforms.ScaledTranslation(10/72, -5/72, fig.dpi_scale_trans)
a[0].text(0.0, 1.0, "a", transform=a[0].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
a[1].text(0.0, 1.0, "b", transform=a[1].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
b[0].text(0.0, 1.0, "c", transform=b[0].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
b[1].text(0.0, 1.0, "d", transform=b[1].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
c[0].text(0.0, 1.0, "e", transform=c[0].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
c[1].text(0.0, 1.0, "f", transform=c[1].transAxes + trans,
        fontsize='medium', verticalalignment='top', fontfamily='serif',
        bbox=dict(facecolor='1.0', edgecolor='none', pad=3.0))
c[0].set_title("left", loc="left")
c[0].set_title("right", loc="right")
c[1].set_title("left", loc="left")
c[1].set_title("right", loc="right")
c[0]._right_title.set_position((0.99, 1))
c[1]._left_title.set_position((0.01, 1))

相关问题