如何向每个 gridspec 组添加标题和副标题

Mat*_*hem 2 python matplotlib title suptitle matplotlib-gridspec

我知道我可以用来update调整 matplotlib 图中实例的参数GridSpec,从而允许在单个图中排列多个 gridspec。就像这个例子取自 matplotlib 文档一样

gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])

gs2 = gridspec.GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = plt.subplot(gs2[:, :-1])
ax5 = plt.subplot(gs2[:-1, -1])
ax6 = plt.subplot(gs2[-1, -1])
Run Code Online (Sandbox Code Playgroud)

但我怎样才能给两者gs1gs2他们自己共同的头衔呢?使用suptitle我只能一次获得整个图形的通用标题。

小智 5

太晚了,但在搜索完全相同的东西时才发现了这个线程,所以把这个留给任何偶然发现它的人。

我认为 @pathoren 的替代方案 4) 是可行的方法,但您可以重用现有的 gridspec 来创建鬼轴,使其与现有的轴完全匹配:

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

fig = plt.figure()
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = fig.add_subplot(gs1[:-1, :])
ax2 = fig.add_subplot(gs1[-1, :-1])
ax3 = fig.add_subplot(gs1[-1, -1])

gs2 = gridspec.GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = fig.add_subplot(gs2[:, :-1])
ax5 = fig.add_subplot(gs2[:-1, -1])
ax6 = fig.add_subplot(gs2[-1, -1])

# Add ghost axes and titles on gs1 and gs2
ax_left = fig.add_subplot(gs1[:])
ax_left.axis('off')
ax_left.set_title('Left title')

ax_right = fig.add_subplot(gs2[:])
ax_right.axis('off')
ax_right.set_title('Right title')

plt.show()
Run Code Online (Sandbox Code Playgroud)

结果布局:

生成的布局