简体   繁体   English

Matplotlib保存到PDF的不同页面

[英]Matplotlib savefig into different pages of a PDF

I have a lengthy plot, composed o several horizontal subplots organized into a column. 我有一个冗长的情节,由几个水平子图组成一个列组成。

When I call fig.savefig('what.pdf'), the resulting output file shows all the plots crammed onto a single page. 当我调用fig.savefig('what.pdf')时,生成的输出文件显示所有填充到单个页面上的图。

Question: is there a way to tell savefig to save on any number (possibly automatically determined) of pdf pages? 问题:有没有办法告诉savefig保存任何数量(可能是自动确定的)pdf页面?

I'd rather avoid multiple files and then os.system('merge ...'), if possible. 如果可能的话,我宁愿避免多个文件,然后是os.system('merge ...')。

I haven't tried myself, but in the matplolib faq there are some instruction to save plots in pdf in several pages. 我没有尝试过自己,但是在matplolib常见问题中,有一些指令可以在几页中保存pdf中的图。

我怀疑有一种更优雅的方法可以做到这一点,但一种选择是使用tempfiles或StringIO来避免在系统上制作传统文件,然后你可以将它们拼凑在一起。

I was wondering how to perform a similar thing. 我想知道如何执行类似的事情。 I have a set plots coming from different image files, which vary depending on the file. 我有来自不同图像文件的设置图,这些图根据文件而有所不同。 So the idea is, once I found a nice number of plots that can be plotted in a page, apply this for the files. 所以我的想法是,一旦我找到了可以在页面中绘制的大量绘图,就可以将其应用于文件。 Luckly, I found a solution suggested here: http://blog.marmakoide.org/?p=94 . 幸运的是,我在这里找到了一个解决方案: http ://blog.marmakoide.org/?p = 94。 However it does not work correctly, since it only plots the first panel plots, leaving the rest of the panels empty. 但它无法正常工作,因为它只绘制第一个面板图,其余面板留空。 I modified it and here I include a working version for a (1XN) grid and the output plots. 我修改了它,在这里我包括一个(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'

第1页

第2页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM