title: Python fitting lognormal to a histogram
body: <p>I'd like to fit a lognormal distribution to some data, but I'm having trouble. I'm trying to use scipy.stats lognorm to fit to my data, I currently have:</p>
  
  <pre><code>from numpy import genfromtxt
  from scipy.stats import lognorm
  import matplotlib at plt
  fig, ax = plt.subplots(1, 1)
  
  data = genfromtext('data.txt')
  plt.hist(data, bins=np.logspace(0,5,200))
  plt.gca().set_xscale("log")
  plt.show()
  
  shape, loc, scale = lognorm.fit(data)
  pdf = sp.stats.lognorm.pdf(data, shape, loc, scale)
  
  plt.plot(data,pdf)
  
  plt.gca().set_xscale("log")
  plt.show()
  </code></pre>
  
  <p>The histogram looks good... <a href="https://i.stack.imgur.com/ySuM1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ySuM1.png" alt="enter image description here"></a> But I get a second plot with the pdf that looks like nonsense:<a href="https://i.stack.imgur.com/aOJtk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aOJtk.png" alt="enter image description here"></a></p>
  
  <p>Once I have the parameters shape, loc and scale, I'd like to use this pdf to randomly generate data that follows this distribution.</p>
  
  <p>The application is a simulation that generates some random objects using <code>np.random.uniform(min,max)</code> but I'd like to use a more realistic distribution. I've found my data file from a reputable source online which represents exactly what I want to generate. So now all I need is the lognormal fit.</p>
  
  <p>Before you ask 'why dont you just randomly pick data from your file?', I would do this, but I want to generate tens of thousands of representative objects, my data file is only 3000 in length.</p>
  
  <p>Any help is appreciated!</p>