Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I save multiple histograms to different (separate) files?

I’m trying to save a bunch of histograms as images. And this is what I did so far.

for i in range(len(images)):
    histogram, bin_edges = np.histogram(images[i].ravel(), 256, [0, 256])
    plt.plot(histogram)
    plt.show()

Output of the above code:

Output of the above code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This output is fine, but I want to save these as images.

I used plt.savefig() but this isn’t the result I’m looking for.

for i in range(len(images)):
    histogram, bin_edges = np.histogram(images[i].ravel(), 256, [0, 256])
    plt.plot(histogram)
    plt.savefig("histograms/hist_frame"+str(count)+".png")
    count+=1

Output of the above code:

output of the above code.

How do I save these histograms in separate files?

>Solution :

Your current code is plotting every histogram on the same figure, In order to have multiple figures, you need to create a new figure for each plot:

for i in range(len(images)):
  plt.figure()
  histogram, bin_edges = np.histogram(images[i].ravel(), 256, [0, 256])
  plt.plot(histogram)
  plt.savefig("histograms/hist_frame"+str(count)+".png")
  count+=1

Note that if showing the plot, plt.show() should follow plt.savefig(), otherwise the file image will be blank.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading