Matplotlib 中的字幕对齐问题

Suptitle alignment issues in Matplotlib

我想在我尝试创建的多个 PDF 文档的每一页的顶部 left-hand 角对齐我的副标题,但是,它似乎并没有像我试图问的那样做.下面的脚本在顶部和中间生成副标题。我不知道我做错了什么:

import datetime
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

pdf = PdfPages('TestPDF.pdf')

Countries = ['Argentina', 'Australia']

for country in Countries:
    fig = plt.figure(figsize=(4, 5))
    fig.set_size_inches([10, 7])
    plt.suptitle(country, horizontalalignment='left', verticalalignment='top',
                 fontsize = 15)
    x = 1000
    plt.plot(x, x*x, 'ko')
    pdf.savefig()
    plt.close(fig)

pdf.close()

奇怪的是:

verticalalignment='bottom'

仅将副标题移得更高(并略微偏离页面顶部),这让我觉得它在与页面边缘不同的边界之外对齐?

来自https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.suptitle.html

Add a centered title to the figure.

kwargs are matplotlib.text.Text properties. Using figure coordinates, the defaults are:

x : 0.5

   The x location of the text in figure coords

y : 0.98

   The y location of the text in figure coords

您可以通过为 x 和 y 选择不同的值来移动字幕在图形坐标中的位置。 例如,

plt.suptitle(country, x=0.1, y=.95, horizontalalignment='left', verticalalignment='top', fontsize = 15)

会将副标题放在左上角。

关键字 horizo​​ntalalignment 和 verticalalignment 的工作方式有点不同(参见 https://matplotlib.org/users/text_props.html):

horizontalalignment controls whether the x positional argument for the text indicates the left, center or right side of the text bounding box.

verticalalignment controls whether the y positional argument for the text indicates the bottom, center or top side of the text bounding box.