2017-06-21 88 views
1

我正在尝试执行以下操作:python matplotlib:在多页中保存为pdf

我已经使用matplotlib创建了一个图,其中包含多个子图。 更具体地说,2x4子图

输出非常适合在屏幕上显示,但不能将其保存为pdf。

如果我只是使用save_fig,它将打印一个单页的pdf文档,并带有2x4网格。

我想要做的是重新安排我的子图,让我们说一个2x4网格(选择哪个子图块在哪里,是好的,但不是必需的),并将其打印成2页的pdf每个子图。 (为了能够适应A4页面尺寸)

这可能吗?

谢谢先进!

+1

我想你将不得不将数字'手动'分成两个并保存到同一个pdf文件。 –

+1

您可以使用'PdfPages'保存多页PDF:https://matplotlib.org/examples/pylab_examples/multipage_pdf.html但您可能需要创建两个图形对象才能做到这一点 – tom

+0

我已经看过多页样本代码,但我想保留当前布局查看,并创建一个新的布局保存。我似乎无法找到从原始图中获得子图的方法,并将它们重新排列为新的子图。 – GSta

回答

2

我建议创建3个数字。一个用于显示,另一个用于保存并绘制相同的数据给他们。

import matplotlib.pyplot as plt 
import numpy as np 


data = np.sort(np.cumsum(np.random.rand(24,16), axis=0), axis=0) 

def plot(ax, x, y, **kwargs): 
    ax.plot(x,y, **kwargs) 

colors = ["crimson", "indigo", "limegreen", "gold"] 
markers = ["o", "", "s", ""] 
lines = ["", "-", "", ":"] 

# figure 0 for showing 
fig0, axes = plt.subplots(nrows=2,ncols=4) 

for i, ax in enumerate(axes.flatten()): 
    plot(ax, data[:,2*i], data[:,2*i+1], marker=markers[i%4], ls=lines[i%4],color=colors[i%4]) 


# figure 1 for saving 
fig1, axes = plt.subplots(nrows=1,ncols=4) 
for i, ax in enumerate(axes.flatten()): 
    plot(ax, data[:,2*i], data[:,2*i+1], marker=markers[i], ls=lines[i],color=colors[i]) 

#figure 2 for saving 
fig2, axes = plt.subplots(nrows=1,ncols=4) 
for i, ax in enumerate(axes.flatten()): 
    plot(ax, data[:,2*i+4], data[:,2*i+1+4], marker=markers[i], ls=lines[i],color=colors[i]) 

#save figures 1 and 2 
fig1.savefig(__file__+"1.pdf") 
fig2.savefig(__file__+"2.pdf") 

#close figures 1 and 2 
plt.close(fig1) 
plt.close(fig2) 
#only show figure 0 
plt.show() 
1

由于我在工作中需要类似的东西,所以我根据显示介质自动化了将图形分组为图形的过程。起初,我的想法是只做一次每个情节,只是将小图添加到数字中以保存在PDF中,但遗憾的是,根据this answer中的评论,这是不可能的,因此所有事情都需要重新绘制。代码示出了如何这可以使用PdfPages被自动化的总体思路:

from matplotlib import pyplot as plt 
import numpy as np 
from matplotlib.backends.backend_pdf import PdfPages 


def niter(iterable, n): 
    """ 
    Function that returns an n-element iterator, i.e. 
    sub-lists of a list that are max. n elements long. 
    """ 
    pos = 0 
    while pos < len(iterable): 
     yield iterable[pos:pos+n] 
     pos += n 


def plot_funcs(x, functions, funcnames, max_col, max_row): 
    """ 
    Function that plots all given functions over the given x-range, 
    max_col*max_row at a time, creating all needed figures while doing 
    so. 
    """ 

    ##amount of functions to put in one plot  
    N = max_col*max_row 

    ##created figures go here 
    figs = [] 

    ##plotted-on axes go here 
    used_axes = [] 

    ##looping through functions N at a time: 
    for funcs, names in zip(niter(functions, N), niter(funcnames,N)): 

     ##figure and subplots 
     fig, axes = plt.subplots(max_col, max_row) 

     ##plotting functions 
     for name,func,ax in zip(names, funcs, axes.reshape(-1)): 
      ax.plot(x, func(x)) 
      ax.set_title(name) 
      used_axes.append(ax) 

     ##removing empty axes: 
     for ax in axes.reshape(-1): 
      if ax not in used_axes: 
       ax.remove() 

     fig.tight_layout() 
     figs.append(fig) 

    return figs 

##some functions to display 
functions = [ 
    lambda x: x, lambda x: 1-x, lambda x: x*x, lambda x: 1/x, #4 
    np.exp, np.sqrt, np.log, np.sin, np.cos,     #5 
    ] 
funcnames = ['x','1-x', 'x$^2$', '1/x', 'exp', 'sqrt', 'log', 'sin','cos'] 

##layout for display on the screen 
disp_max_col = 3 
disp_max_row = 2 

##layout for pdf 
pdf_max_col = 2 
pdf_max_row = 4 

##displaying on the screen: 
x = np.linspace(0,1,100) 
figs = plot_funcs(x, functions, funcnames, disp_max_row, disp_max_col) 
plt.show() 


##saving to pdf if user wants to: 
answer = input('Do you want to save the figures to pdf?') 
if answer in ('y', 'Y', 'yes', ''): 

    ##change number of subplots 
    N = disp_max_col*disp_max_row 
    figs = plot_funcs(x, functions, funcnames, pdf_max_row, pdf_max_col) 

    ##from https://matplotlib.org/examples/pylab_examples/multipage_pdf.html 
    with PdfPages('multipage_pdf.pdf') as pdf: 
     for fig in figs: 
      plt.figure(fig.number) 
      pdf.savefig() 

的核心功能,plot_funcs需要max_colmax_row关键字,然后创建数字与副区的根据量。然后循环遍历给定的函数列表,并将其绘制在每个函数的子图上。未使用的子图被删除。最后返回所有数字的列表。

在我的例子中,我有9种不同的功能,我首先在屏幕上以2x3布局显示(总共有两个数字,一个带有6个子图,另一个带有3个子图)。如果用户很满意,则以2×4布局(也是两个数字,但这次是一个包含8个子图,1个包含1个子图)重新绘制图,然后保存到multipage_pdf.pdf之后的example in the documentation之后的文件中。

python 3.5测试