我有一个冗长的情节,由几个水平的子情节组成,组织成一列。
当我调用 fig.savefig('what.pdf') 时,生成的输出文件会显示所有图都挤在一个页面上。
问题:有没有办法告诉 savefig 保存任何数量(可能是自动确定)的 pdf 页面?
如果可能的话,我宁愿避免使用多个文件,然后使用 os.system('merge ...')。
我有一个冗长的情节,由几个水平的子情节组成,组织成一列。
当我调用 fig.savefig('what.pdf') 时,生成的输出文件会显示所有图都挤在一个页面上。
问题:有没有办法告诉 savefig 保存任何数量(可能是自动确定)的 pdf 页面?
如果可能的话,我宁愿避免使用多个文件,然后使用 os.system('merge ...')。
我自己没有尝试过,但是在 matplolib 常见问题解答中有一些说明可以在几页中以 pdf 格式保存绘图。
我怀疑有一种更优雅的方法可以做到这一点,但一种选择是使用 tempfiles 或 StringIO 来避免在系统上制作传统文件,然后您可以将它们拼凑在一起。
我想知道如何执行类似的事情。我有一组来自不同图像文件的图,这些图因文件而异。所以这个想法是,一旦我发现可以在页面中绘制的大量绘图,将其应用于文件。幸运的是,我在这里找到了建议的解决方案: http ://blog.marmakoide.org/?p=94 。但是它不能正常工作,因为它只绘制第一个面板图,其余面板为空。我修改了它,在这里我包含了一个(1XN)网格和输出图的工作版本。
import numpy
from matplotlib import pyplot as plot
from matplotlib.backends.backend_pdf import PdfPages
# Generate the data
data = numpy.random.randn(7, 1024)
# The PDF document
pdf_pages = PdfPages('histograms.pdf')
# Generate the pages
nb_plots = data.shape[0]
nb_plots_per_page = 5
nb_pages = int(numpy.ceil(nb_plots / float(nb_plots_per_page)))
grid_size = (nb_plots_per_page, 1)
for i, samples in enumerate(data):
print
print i,i % nb_plots_per_page,samples
# Create a figure instance (ie. a new page) if needed
if i % nb_plots_per_page == 0:
print 'Opening'
fig = plot.figure(figsize=(8.27, 11.69), dpi=100)
# Close the page if needed
elif (i + 1) % nb_plots_per_page == 0 or (i + 1) == nb_plots:
plot.subplot2grid(grid_size, (i % nb_plots_per_page, 0))
plot.hist(samples, 32, normed=1, facecolor='#808080', alpha=0.75)
plot.title(str(i+1))
plot.tight_layout()
pdf_pages.savefig(fig)
print 'Closing'
print i,samples
# Plot stuffs !
print i,samples
plot.subplot2grid(grid_size, (i % nb_plots_per_page, 0))
plot.hist(samples, 32, normed=1, facecolor='#808080', alpha=0.75)
plot.title(str(i+1))
# Write the PDF document to the disk
pdf_pages.close()
print 'histograms.pdf'