在 Julia 中将图形输出重定向到多页 PDF

Redirect graphical output to multi-page PDF in Julia

我想将 Julia 语言脚本的所有图形输出重定向到单个多页 PDF 文件,类似于 R 中的内容:

pdf(foo.pdf) 
plot(dummy1) 
plot(dummy2)
... 
plot(dummyn)
dev.off() 

通过

重定向输出流
writemime(io, "application/pdf", plot) 

适用于单个文件,但它似乎无法以某种方式附加到现有文件。

请注意,我只想将脚本中的所有数字都转换成一个 pdf。我不想通过 weave 做大量的报告。

有什么想法吗?

我通常使用 matplotlib 的 PdfPages 后端,我猜这是一个作弊答案,因为它使用 python 但无论如何这里有一个例子。

例如

using PyCall, PyPlot
@pyimport matplotlib.backends.backend_pdf as pdf

figures = [] # this array will contain all of the figures
for i in 1:2
    f = figure(figsize=(11.69,8.27)) # A4 figure
    ax = subplot(111)
    ax[:plot](rand(5,100)) # random plot
    push!(figures,f) # add figure to figures array
end

pdffile = pdf.PdfPages("fig.pdf") # create pdf file
[pdffile[:savefig](f) for f in figures] # add figures to file
pdffile[:close]() # close pdf file