我想要保存两个数字在脚本的不同部分创建成一个pdf使用PdfPages,它可以把它们附加到PDF吗?
示例:
fig = plt.figure()
ax = fig_zoom.add_subplot(111)
ax.plot(range(10), range(10), 'b')
with PdfPages(pdffilepath) as pdf:
pdf.savefig(fig)
fig1 = plt.figure()
ax = fig_zoom.add_subplot(111)
ax.plot(range(10), range(2, 12), 'r')
with PdfPages(pdffilepath) as pdf:
pdf.savefig(fig1)
发布于 2014-12-11 09:18:48
抱歉,这问题太烂了。我们只是不应该使用with
语句。
fig = plt.figure()
ax = fig_zoom.add_subplot(111)
ax.plot(range(10), range(10), 'b')
# create a PdfPages object
pdf = PdfPages(pdffilepath)
# save plot using savefig() method of pdf object
pdf.savefig(fig)
fig1 = plt.figure()
ax = fig_zoom.add_subplot(111)
ax.plot(range(10), range(2, 12), 'r')
pdf.savefig(fig1)
# remember to close the object to ensure writing multiple plots
pdf.close()
发布于 2017-07-06 16:29:04
我认为Prashanth's answer可以得到更好的推广,例如通过将它合并到一个for循环中,并避免创建多个数字( can generate memory leaks )。
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
# create a PdfPages object
pdf = PdfPages('out.pdf')
# define here the dimension of your figure
fig = plt.figure()
for color in ['blue', 'red']:
plt.plot(range(10), range(10), color)
# save the current figure
pdf.savefig(fig)
# destroy the current figure
# saves memory as opposed to create a new figure
plt.clf()
# remember to close the object to ensure writing multiple plots
pdf.close()
发布于 2017-08-10 18:13:59
如果文件已经关闭,这些选项都不会附加(例如,在程序的一次执行中创建该文件,然后再次运行该程序)。在该用例中,它们都覆盖文件。
我认为目前不支持追加。查看backend_pdf.py
的代码,我看到:
class PdfFile(object)
...
def __init__(self, filename):
...
fh = open(filename, 'wb')
因此,函数始终是写的,而不是附加的。
https://stackoverflow.com/questions/27419001
复制相似问题