简体   繁体   English

如何将多个直方图保存到多个pdf文件中

[英]How to save multiple histogram plots into multiple pdf files

I'm having trouble saving histogram plots from a "for loop" into multiple pdf files. 我在将直方图从“ for循环”保存到多个pdf文件时遇到了麻烦。

I have tried the .savefig() and the img2pdf. 我已经尝试过.savefig()和img2pdf。

for i,title in enumerate(titles):
    count, bins, ignored = plt.hist(dists[i], 50, normed=True, range= 
    [450,1700])
    plt.title(title)
    plt.xlabel("g CO2-eq/day-bed " )
    plt.ylabel("Frequency")
    plt.show()

    plt.savefig[i]("nitrileglob1.pdf",bbox_inches='tight')

I either save one plot or none of them get saved. 我要么保存一个情节,要么都不保存。 I want to save each of the dists[i] into a pdf file. 我想将每个dists [i]保存到pdf文件中。 The last line is not really working... 最后一行实际上没有用...

The problem is that you are saving all of your plots under the same name. 问题是您要以相同的名称保存所有图解。 You would have to change the file name in each iteration, for example 例如,您必须在每次迭代中更改文件名。

plt.savefig("nitrileglob" + str(i) + ".pdf",bbox_inches="tight")

You should also save the file before calling plt.show() , as this clears the current figure. 您还应该在调用plt.show()之前保存文件,因为这样可以清除当前图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM