按照官方文档的这个example,我可以在不同的页面中创建一个pdf文件,其中包含我想要的绘图。但我想在页面中添加一些文本(而不是在绘图中),我已经尝试过这种方法,但没有成功:
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()怎样才能同时显示文本this is an example?是否也可以创建一个只包含文本的第一个页面?提前感谢
发布于 2018-03-23 18:10:57
文本'this is an example'被放置在数据坐标中的位置(1,1)。如果您的数据范围不同,则可能超出了绘图范围。在图形坐标中指定它是有意义的。它们的范围从0到1,其中0,0是左下角,1,1是右上角。例如。
plt.text(0.05,0.95,txt, transform=fig.transFigure, size=24)这个例子
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()创建此图

您不能创建空的pdf页面。当然,您可以通过创建一个没有内容的图形或一个只有文本的空图形来模仿一个图形。
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()

https://stackoverflow.com/questions/49444008
复制相似问题