How to Append a Plot to an Existing PDF File

How to append a plot to an existing pdf file

You could use recordPlot to store each plot in a list, then write them all to a pdf file at the end with replayPlot. Here's an example:

num.plots <- 5
my.plots <- vector(num.plots, mode='list')

for (i in 1:num.plots) {
plot(i)
my.plots[[i]] <- recordPlot()
}
graphics.off()

pdf('myplots.pdf', onefile=TRUE)
for (my.plot in my.plots) {
replayPlot(my.plot)
}
graphics.off()

append page to existing pdf file using python (and matplotlib?)

You may want to use pyPdf for this.

# Merge two PDFs
from PyPDF2 import PdfFileReader, PdfFileWriter

output = PdfFileWriter()
pdfOne = PdfFileReader(open("path/to/pdf1.pdf", "rb"))
pdfTwo = PdfFileReader(open("path/to/pdf2.pdf", "rb"))

output.addPage(pdfOne.getPage(0))
output.addPage(pdfTwo.getPage(0))

outputStream = open(r"output.pdf", "wb")
output.write(outputStream)
outputStream.close()

example taken from here

Thereby you detach the plotting from the pdf-merging.

append pdf file in R - dev.off()

... and all putting all commands from above together

require(ggplot2)
require(gridExtra)
TopFolder <-"...directory on my drive"
setwd(TopFolder)

pdf(file = file.path(TopFolder,"figures","example.pdf"), onefile=TRUE)
g <- list()
for(i in 1:4){
dat <- data.frame(d1 = c(1:10),
d2 = runif(10))
g[[i]] <- qplot(x = d1, y = d2,
data = dat)
}
grid.arrange(g[[1]],g[[2]],g[[3]],g[[4]])

gg <- list()

# each of this plots will be on a separate page
for(i in 1:6){
dat <- data.frame(d1 = c(1:20),
d2 = runif(20))

# here a print is necessary
print(qplot(x = d1, y = d2,
data = dat))
}
dev.off()

is it possible to append figures to Matplotlib's PdfPages?

Sorry, that's a lame question. We just shouldn't use the with statement.

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()


Related Topics



Leave a reply



Submit