11

I have following code that generates a histogram. How can I save the histogram automatically using the code? I tried what we do for other plot types but that did not work for histogram.a is a 'numpy.ndarray'.

a = [-0.86906864 -0.72122614 -0.18074998 -0.57190212 -0.25689268 -1.
     0.68713553  0.29597819  0.45022949  0.37550592  0.86906864  0.17437203
     0.48704826  0.2235648   0.72122614  0.14387731  0.94194514 ]

fig = pl.hist(a,normed=0)
pl.title('Mean')
pl.xlabel("value")
pl.ylabel("Frequency")
pl.savefig("abc.png")
A.S
  • 305
  • 1
  • 4
  • 20

1 Answers1

14

This works for me:

import matplotlib.pyplot as pl
import numpy as np

a = np.array([-0.86906864, -0.72122614, -0.18074998, -0.57190212, -0.25689268 ,-1. ,0.68713553 ,0.29597819, 0.45022949, 0.37550592, 0.86906864, 0.17437203, 0.48704826, 0.2235648, 0.72122614, 0.14387731, 0.94194514])

fig = pl.hist(a,normed=0)
pl.title('Mean')
pl.xlabel("value")
pl.ylabel("Frequency")
pl.savefig("abc.png")

a in the OP is not a numpy array and its format also needs to be modified (it needs commas, not spaces as delimiters). This program successfully saves the histogram in the working directory. If it still does not work, supply it with a full path to the location where you want to save it like this

pl.savefig("/Users/atru/abc.png")

The pl.show() statement should not be placed before savefig() as it creates a new figure which makes savefig() save a blank figure instead of the desired one as explained in this post.

zdim
  • 64,580
  • 5
  • 52
  • 81
atru
  • 4,699
  • 2
  • 18
  • 19
  • @ atru: Yes, I am using matplotlib. I tried the way you suggested but it saves an empty png file. – A.S Sep 25 '17 at 18:27
  • Can you share more of your code then? It works perfect for me. In fact I was pretty happy with the quality. – atru Sep 25 '17 at 18:28
  • @ atru: I have edited the code in the question section with savefig. Please take a look. Thank you – A.S Sep 25 '17 at 18:32
  • Can you also add what is `a`? – atru Sep 25 '17 at 18:33
  • Works for me (your complete sequence) for `a = np.random.randn(1000)`. Try specifying the full path to the directory where you want to save the file. – atru Sep 25 '17 at 18:37
  • I just added information about a in the question. Would you please check. – A.S Sep 25 '17 at 18:41
  • Is this your real code? Because if it is then it should throw an error. I fixed it, see my updated answer. – atru Sep 25 '17 at 18:47
  • Yes, this is a real code. I am not sure why but this still does not works on my side. a is 'numpy.ndarray' will this matter? – A.S Sep 25 '17 at 18:58
  • Did you use exactly what I'm using? Including the redefinition of `a`? And after that did you try with the full path? – atru Sep 25 '17 at 19:03
  • Yes, I tried exactly what you're using. I am not sure how to try full path? Sorry I am a beginner. – A.S Sep 25 '17 at 19:08
  • It's ok, this is odd - it really does work for me. What system are you on? The full path means the path to the directory you want to save the file into. For instance `D:\atru\data\ ` on Windows or the example above on Mac or Linux. Also, instead of saving, run `pl.show()` to simply see if it's plotting anything at all. – atru Sep 25 '17 at 19:13
  • I ran pl.show() and it shows perfect. I am on windows. It even creates an empty png file. – A.S Sep 25 '17 at 19:21
  • 3
    Briefly scrolling through [this](https://stackoverflow.com/questions/9012487/matplotlib-pyplot-savefig-outputs-blank-image) post and checking it out - do you have a `pl.show()` before `pl.savefig()` ? – atru Sep 25 '17 at 19:26
  • 2
    I just realized this too. I have pl.show() before pl.savefig(). I switched those lines of code and now it works fine. Thank you very much. – A.S Sep 25 '17 at 19:33