0

我正在尝试将我的输出(70 多张图表)从 jyputer 笔记本保存到 pdf 文件中。没有错误或任何错误,但只有第一个图表被导出。谁能帮我理解什么是错的?

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

pdf = matplotlib.backends.backend_pdf.PdfPages("output.pdf")
df_num = df.select_dtypes(include = ['float64', 'int64'])

with PdfPages(r'output.pdf') as export_pdf:
    fig = plt.figure(figsize=(12,12))
    for col in df_num.columns:
        hist = df_num[col].hist(bins=10)
        print("Plotting for column {}".format(col))
        plt.title("{}".format(col), fontsize=20) 
        plt.show()        
    export_pdf.savefig(fig)       
4

1 回答 1

1

好的,最终我想通了。这是我的解决方案:

with PdfPages(r'output_dist.pdf') as export_pdf:
    
    for col in df_f.columns:
        fig = plt.figure(figsize=(12,12))
        hist = df_f[col].hist(bins=10)
        print("Plotting for column {}".format(col))
        plt.title("{}".format(col), fontsize=20) 
        plt.show()
        
        export_pdf.savefig(fig)
        
于 2020-11-11T18:39:15.630 回答