是否可以有多个字幕?

bla*_*kbk 2 python matplotlib

如果我有一个带有子图的 matplotlib 图形,是否可以有多个子图suptitle?(有点像常规标题的 loc='center'、loc='left' 和 loc='right' 参数)。

在下面的示例中, 的多个实例suptitle被覆盖,并且图中仅显示最后一个字幕。

import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1,2)

plt.suptitle('center title')
plt.suptitle('right title', x=.8)
plt.suptitle('left title', x=.2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

bla*_*kbk 5

我想出的最佳解决方案是将文本添加到图中,并根据带有参数的图形坐标调整位置transform=fig.transFigure。

import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1,2)

plt.text(.5, 1, 'center title', transform=fig.transFigure, horizontalalignment='center')
plt.text(.8, 1, 'right title', transform=fig.transFigure, horizontalalignment='center')
plt.text(.2, 1, 'left title', transform=fig.transFigure, horizontalalignment='center')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述