首页 > 解决方案 > 从pdf中的循环中保存多个图

问题描述

我有大型数据集,我在其中绘制和拟合循环 x(df_time)vs y(df_intensity 中的所有列。我想保存所有通过拟合生成的图,但我似乎无法管理,我只能设法显示它们。

我还在情节下方得到一条线,可能表明问题所在,但我找不到替代方案。有没有一种方法可以轻松实现另存为 pdf 的方式,即每页获得多个图(我正在生成数百个,所以我想我将需要多个页面,所以理想情况下是自动将其保存在 pdf 中并且当页面已满,请转到下一页。非常感谢!

fname must be a PathLike or file handle

我正在使用以下代码:

coefficients=[]
for column in df_intensity:
    try:
        plt.scatter(df_time01, column)
        plt.show()
# starting point for the decay to fit
        decay_start = np.argmax(column)
# use only decay points in the fit
        guess = [1, 0.001, 0]

        popt, pcov = curve_fit(func, df_time01[decay_start:],
                           column[decay_start:],
                           p0=guess)

# get the fit curve
    #try:
        fit = func(df_time01[decay_start:], *popt)

# plot the fit and data
        plt.plot(df_time01[decay_start:], fit, label='fit', c='r')
        plt.scatter(df_time01, column, label='data')
        plt.legend()
        plt.show()
        print('Fit parameters: {}'.format(popt))
        coefficients.append(popt)
        plt.savefig(column, dpi=300, transparent=True)  
        with PdfPages('multipage_pdf.pdf') as pdf:
            plt.figure(figsize=(3, 3))
            plt.plot
            plt.title('Page One')
            pdf.savefig()  # saves the current figure into a pdf page
            plt.close()
    except Exception as e:
        print(e)
        print("Error - curve_fit failed")
        continue

标签: pythonpdfplotsave

解决方案


推荐阅读