【问题标题】:How to save multiple Seaborn plots into single pdf file如何将多个 Seaborn 图保存到单个 pdf 文件中
【发布时间】:2018-06-10 11:21:02
【问题描述】:

所以我试图将我在 for 循环中创建的多个图保存到一个 pdf 文件中。我在 SO 上进行了搜索,并拼凑了一些似乎可以工作的代码,除了它不保存创建 pdf 但没有任何内容的数字。

这是重现它的代码:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

dftest = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),
                    columns=['a', 'b', 'c', 'd', 'e']) 

from matplotlib.backends.backend_pdf import PdfPages

with PdfPages('count.pdf') as pdf_pages:
    df1 = dftest.select_dtypes([np.int, np.float, np.object])
    for i, col in enumerate(df1.columns):
        plt.figure(i)
        countplot = sns.countplot(x=col, data=df1)
        pdf_pages.savefig(countplot.fig)

【问题讨论】:

    标签: python plot seaborn


    【解决方案1】:

    保存 plt.figure 对我有用

    with PdfPages('count.pdf') as pdf_pages:
        df1 = dftest.select_dtypes([np.int, np.float, np.object])
        for i, col in enumerate(df1.columns):
            figu = plt.figure(i)
            countplot = sns.countplot(x=col, data=df1)
            pdf_pages.savefig(figu)
    

    【讨论】:

    • 我的 N00b 错误没有将 figu 分配给 plt.figure(i)。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 2015-11-21
    • 2013-07-21
    • 1970-01-01
    • 2012-07-04
    相关资源
    最近更新 更多