首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python matplotlib:多页保存为pdf

Python matplotlib:多页保存为pdf
EN

Stack Overflow用户
提问于 2017-06-21 16:50:44
回答 2查看 8.2K关注 0票数 1

我正在尝试做以下几件事:

我已经使用matplotlib创建了一个带有几个子图的图形。更具体地说,2x4子图

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

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

我想要做的,是重新排列我的子图,比方说一个2x4的网格(选择哪个子图会很好,但不是必须的),并将其打印到一个2页的pdf中,每个有4个子图。(为了能够使其适合A4页面大小)

这个是可能的吗?

非常感谢您的支持!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-21 18:13:02

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

代码语言:javascript
复制
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()
票数 2
EN

Stack Overflow用户

发布于 2017-06-22 17:21:33

因为我的工作需要类似的东西,所以我花了一些精力来自动化将绘图分组为图形的过程,这取决于显示介质。起初,我的想法是每个图只做一次,然后将子图添加到要保存在pdf中的图形中,但不幸的是,根据this answer中的一条评论,这是不可能的,所以所有的东西都需要重新绘制。代码显示了如何使用PdfPages自动执行此操作的一般思路

代码语言:javascript
复制
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个子图)。如果用户满意,则在2x4布局中重做绘图(也是两个图形,但这次一个有8个子图,1个有1个子图),然后按照example in the documentation保存到名为multipage_pdf.pdf的文件中。

在python 3.5上测试

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44671169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档