【问题标题】:Suptitle alignment issues in MatplotlibMatplotlib 中的字幕对齐问题
【发布时间】:2017-10-26 11:20:30
【问题描述】:

我想在我尝试创建的多个 PDF 文档的每一页的左上角对齐我的副标题,但是,它似乎没有按照我的要求进行。下面的脚本在顶部和中心生成字幕。我无法弄清楚我做错了什么:

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'

仅将字幕移得更高(并略微偏离页面顶部),这让我认为它正在将自身与页面边缘不同的边界对齐?

【问题讨论】:

    标签: python matplotlib suptitle


    【解决方案1】:

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

    为图形添加居中的标题。

    kwargs 是 matplotlib.text.Text 属性。使用图形坐标,默认为:

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

    horizo​​ntalalignment 控制文本的 x 位置参数是指示文本边界框的左侧、中心还是右侧。

    verticalalignment 控制文本的 y 位置参数是否指示文本边界框的底部、中心或顶部。

    【讨论】:

    • 谢谢 - 这正在工作。所以我想我对原始参数的理解不正确 - ha 和 va 将根据字幕所在的坐标进行对齐,但是需要 x 和 y 来实际设置布局上的大致位置?
    • 是的,完全正确。我在答案中添加了有关这些关键字的更多详细信息。
    • 关于这方面的原始文档似乎强调要掩盖所有这些使用细节
    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 2011-06-26
    相关资源
    最近更新 更多