是否可以有多个suptitle?

2024-03-28 12:10:00 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有一个带有子图的matplotlib图,是否可能有多个suptitle?(Kind of like the loc='center', loc='left', and loc='right' arguments for a regular title

在下面的示例中,suptitle的多个实例被覆盖,并且只有最后一个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)

enter image description here


Tags: andoftherightfortitlematplotlibplt
1条回答
网友
1楼 · 发布于 2024-03-28 12:10:00

我想到的最好的解决方案是在绘图中添加文本,并根据带有参数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')

enter image description here

相关问题 更多 >