多个子图的标题(suptitle)

4
我有一个3x3的网格,包含9个子图,每个子图都有一个标题。我想为每一行添加一个标题。为此,我考虑使用suptitle。但问题是,如果我使用3个suptitle,它们似乎会被覆盖,只有最后一个会显示。
以下是我的基本代码:
fig, axes = plt.subplots(3,3,sharex='col', sharey='row')

for j in range(9):
    axes.flat[j].set_title('plot '+str(j))

plt1 = fig.suptitle("row 1",x=0.6,y=1.8,fontsize=18)
plt2 = fig.suptitle("row 2",x=0.6,y=1.2,fontsize=18)
plt3 = fig.suptitle("row 3",x=0.6,y=0.7,fontsize=18)
fig.subplots_adjust(right=1.1,top=1.6)

enter image description here


为什么不只是设置轴1、4、7(j%3 == 1)的标题并清除其余部分呢? - wflynny
因为我仍然需要每个子图的标题,以及一行的标题。 - Alessandro
每个图形只能有一个suptitle。我建议使用text对象作为行标题。 - tmdavison
我觉得我会选择这个 - Alessandro
1个回答

4
您可以调整标题和标签。请参考以下示例,该示例已从您的代码进行了修改:
import matplotlib.pyplot as plt

fig, axes = plt.subplots(3,3,sharex='col', sharey='row')

counter = 0
for j in range(9):
    if j in [0,3,6]:
        axes.flat[j].set_ylabel('Row '+str(counter), rotation=0, size='large',labelpad=40)
        axes.flat[j].set_title('plot '+str(j))
        counter = counter + 1
    if j in [0,1,2]:
        axes.flat[j].set_title('Column '+str(j)+'\n\nplot '+str(j))
    else:
        axes.flat[j].set_title('plot '+str(j))

plt.show()

这将导致:

行和列标题


这是一个很好的修复,尽管考虑到我的标题有点长,不太可行。 - Alessandro
@Alessandro 你可以管理标签的字体大小,使其更加整洁。这种方法的一个大问题是,如果你想给列和标题等不同的格式,那么就会很麻烦。 - armatita
很不幸,我的子图没有纵轴标签。我在这里发布了一个类似的问题,以寻求另一种解决方法。 - Daniel Himmelstein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接