带有子图的多个标题(suptitle)

时间:2016-04-13 14:58:53

标签: python matplotlib title subplot

我在3x3网格中有一系列9个子图,每个子图都有一个标题。 我想为每一行添加一个标题。为此我想到了使用suptitle。 问题是,如果我使用3个suptitles,它们似乎被覆盖,只有最后一个似乎被显示出来。

这是我的基本代码:

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 个答案:

答案 0 :(得分: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()

,结果是:

Row and Columns titles

相关问题