title: Gaussian does not fit correctly to data
body: <p>I am trying to fit a gaussian curve to my data which is a list of density variations with height, however the plot of the fitted curve generated is always off (peak doesn't align, width is overestimated). Here is my code:</p>
  
  <pre><code>import pandas as pd
  import matplotlib.pyplot as plt
  import numpy as np
  from scipy.optimize import curve_fit
  
  #Gaussian function
  def gauss_function(x, a, x0, sigma):
      return a*np.exp(-(x-x0)**2/float((2*sigma**2)))
  
  x = heights5
  y = demeans5 #density values at each height
  
  amp = max(y)
  center = x[np.argmax(y)]
  width = 20 #eye-balled estimate
  
  
  #p0 = amp, width, center
  popt, pcov = curve_fit(gauss_function, x, y, p0 = [amp, width, center])
  
  #plot
  dataplot = plt.scatter(x, y, marker = '.', label = 'Observations')
  gausplot = plt.plot(x,gauss_function(x, *popt), color='red', label ='Gaussian fit')
  
  string = 'fwhm = ' + str(2.355*popt[2]) + '\npeak = ' + str(popt[0]) + '\nmean = ' + str(popt[1]) + '\nsigma = ' + str(popt[2])
  
  #plot labels etc.
  plt.xlabel("Height[km]")
  plt.ylabel("Density")
  plt.legend([dataplot, gausplot], labels = ['fit', 'Observations'])
  plt.text(130, 2000, string)
  plt.show()
  </code></pre>
  
  <p>This is the plot it generates:</p>
  
  <p><a href="https://i.stack.imgur.com/ZsQda.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZsQda.png" alt="poor fit"></a></p>
  
  <p>How do I fit the curve more accurately? And also, is there a way to estimate the width with the data?</p>