Cur*_*ent 4 python matplotlib suptitle
我想在我试图创建的多个PDF文档的每个页面的左上角对齐我的suptitle,但是,它似乎没有按照我的要求进行操作.下面的脚本在顶部和中心生成suptitle.我无法弄清楚我做错了什么:
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()
Run Code Online (Sandbox Code Playgroud)
奇怪的是:
verticalalignment='bottom'
Run Code Online (Sandbox Code Playgroud)
只是将suptitle移动得更高(并略微偏离页面顶部),这让我觉得它是在与页面边缘不同的边界上对齐自己?
jda*_*amp 11
来自https://matplotlib.org/devdocs/api/_as_gen/matplotlib.pyplot.suptitle.html:
为图中添加居中标题.
kwargs是matplotlib.text.Text属性.使用图形坐标,默认值为:
x:0.5
Run Code Online (Sandbox Code Playgroud)The x location of the text in figure coords
y:0.98
您可以通过为x和y选择不同的值来移动图标坐标中的suptitle的位置.例如,
plt.suptitle(country, x=0.1, y=.95, horizontalalignment='left', verticalalignment='top', fontsize = 15)
将suptitle放在左上角.
关键字horizontalalignment和verticalalignment的工作方式略有不同(参见https://matplotlib.org/users/text_props.html):
horizontalalignment控制文本的x位置参数是指示文本边界框的左侧,中间还是右侧.
verticalalignment控制文本的y位置参数是指示文本边界框的底部,中心还是顶部.