2

I'm having trouble saving histogram plots from a "for loop" into multiple pdf files.

I have tried the .savefig() and the 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. The last line is not really working...

YellowAfterlife
  • 2,967
  • 1
  • 16
  • 24
JEREMIAH
  • 21
  • 1

1 Answers1

0

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.

ggian
  • 76
  • 6