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

问题描述 投票:0回答:2

我知道我可以使用

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])

但是我怎样才能给

gs1
gs2
他们自己的共同标题呢?使用
suptitle
我一次只能得到整个人物的共同标题。

python matplotlib title matplotlib-gridspec suptitle
2个回答
3
投票

我能想到四种方法,都很难看。我不知道是否有任何自动设置这些东西的方法。

四种丑陋的方式是:

1)使用

ax.set_title()
(在您的情况下为
ax1
ax4
)将标题设置为每个组中的“顶部”轴对象。它在左组上效果很好,但对右组来说很糟糕......

2) 设置一个标题用

fig.suptitle
,但是在标题里面留很多空格,用
horizontalalignment='center'
.

3) 为每个标题手动设置一个文本对象...(不是在下面的示例中,而是只看matplotlib.text

4) 创建幽灵斧头,隐藏上面的所有东西,只用它们来设置它们的标题...

下面是一些示例代码

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])
ax1.set_title('Left group title')  # Alternative 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])
ax4.set_title('Right group title')  # Alternative 1)
# Alternative 2. Note the many white-spaces
fig.suptitle('figure title left                                                   figure title right', horizontalalignment='center')

# Alternative 4)
rect_left = 0, 0, 0.5, 0.8  # lower, left, width, height (I use a lower height than 1.0, to place the title more visible)
rect_right = 0.5, 0, 0.5, 0.8
ax_left = fig.add_axes(rect_left)
ax_right = fig.add_axes(rect_right)
ax_left.set_xticks([])
ax_left.set_yticks([])
ax_left.spines['right'].set_visible(False)
ax_left.spines['top'].set_visible(False)
ax_left.spines['bottom'].set_visible(False)
ax_left.spines['left'].set_visible(False)
ax_left.set_axis_bgcolor('none')
ax_right.set_xticks([])
ax_right.set_yticks([])
ax_right.spines['right'].set_visible(False)
ax_right.spines['top'].set_visible(False)
ax_right.spines['bottom'].set_visible(False)
ax_right.spines['left'].set_visible(False)
ax_right.set_axis_bgcolor('none')
ax_left.set_title('Ghost left title')
ax_right.set_title('Ghost right title')

plt.show()


3
投票

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

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

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()

结果布局:

© www.soinside.com 2019 - 2024. All rights reserved.