[英]Add text with PdfPages - matplotlib
Following this example of the official documentation, I can create a pdf file with the plots that I want in different pages. 按照官方文档的这个例子 ,我可以在不同的页面中创建一个包含我想要的图的pdf文件。 But I would like to add some text to the page(not inside the plot) and I've tried in this way without success: 但是我想在页面中添加一些文本(不在图中)并且我已经尝试过这种方式而没有成功:
with PdfPages('multipage_pdf.pdf') as pdf:
fig = plt.figure(figsize=(11.69,8.27))
x = df1.index
y1 = df1[col1]
y2 = df1[col2]
plt.plot(x, y1, label=col1)
plt.plot(x, y2, label=col2)
plt.legend(loc='best')
plt.grid(True)
plt.title('Title')
txt = 'this is an example'
plt.text(1,1,txt)
pdf.savefig()
plt.close()
How can I show also the text this is an example
? 我怎样才能显示文本this is an example
? Is it possible to create also a first page with only text? 是否可以创建仅包含文本的第一页? Thanks in advance 提前致谢
The text 'this is an example'
is placed at position (1,1)
in data coordinates. 文本'this is an example'
放在数据坐标中的位置(1,1)
。 If your data range is different, it might be out of the plot. 如果您的数据范围不同,则可能不在图中。 It would make sense to specify it in figure coordinates. 在图形坐标中指定它是有意义的。 Those range from 0 to 1, where 0,0 is the lower left corner and 1,1 is the upper right corner. 范围从0到1,其中0,0是左下角,1,1是右上角。 Eg 例如
plt.text(0.05,0.95,txt, transform=fig.transFigure, size=24)
This example 这个例子
import datetime
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
with PdfPages('multipage_pdf.pdf') as pdf:
fig = plt.figure(figsize=(11.69,8.27))
plt.plot([1,2,3], [1,3,2], label="col1")
plt.plot([1,2,3], [2,1,3], label="col2")
plt.legend(loc='best')
plt.grid(True)
plt.title('Title')
txt = 'this is an example'
plt.text(0.05,0.95,txt, transform=fig.transFigure, size=24)
pdf.savefig()
plt.close()
creates this plot 创造这个情节
You cannot create an empty pdf page. 您无法创建空的pdf页面。 But of course you can mimic one by creating a figure without content, or an empty figure with just text. 但是当然你可以通过创建一个没有内容的数字来模仿一个,或者只是一个带有文本的空图形。
import datetime
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
with PdfPages('multipage_pdf.pdf') as pdf:
firstPage = plt.figure(figsize=(11.69,8.27))
firstPage.clf()
txt = 'This is the title page'
firstPage.text(0.5,0.5,txt, transform=firstPage.transFigure, size=24, ha="center")
pdf.savefig()
plt.close()
fig = plt.figure(figsize=(11.69,8.27))
plt.plot([1,2,3], [1,3,2], label="col1")
plt.plot([1,2,3], [2,1,3], label="col2")
plt.legend(loc='best')
plt.grid(True)
plt.title('Title')
txt = 'this is an example'
plt.text(0.05,0.95,txt, transform=fig.transFigure, size=24)
pdf.savefig()
plt.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.