I have a dark image (raw format), and plotted the image and distribution of the image. As you can see, there is a peak at 16, please ignore that. I want to fit at gaussian curve through this histogram. I've used this method to fit:
Un-normalized Gaussian curve on histogram. However; my Gaussian fit never comes close to what it supposed to be. Am I doing something wrong with turning the image into the correct format for the plot or is something else going wrong?

This is the current code I use to generate this data:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def fitGaussian(x,a,mean,sigma):
return (a*np.exp(-((x-mean)**2/(2*sigma))))
fname = 'filepath.raw'
im = np.fromfile(fname,np.int16)
im.resize([3056,4064])
plt.figure()
plt.set_cmap(viridis)
plt.imshow(im, interpolation='none', vmin=16, vmax=np.percentile(im.ravel(),99))
plt.colorbar()
print 'Saving: ' + fname[:-4] + '.pdf'
plt.savefig(fname[:-4]+'.pdf')
plt.figure()
data = plt.hist(im.ravel(), bins=4096, range=(0,4095))
x = [0.5 * (data[1][i] + data[1][i+1]) for i in xrange(len(data[1])-1)]
y = data[0]
popt, pcov = curve_fit(fitGaussian, x, y, [500000,80,10])
x_fit = py.linspace(x[0], x[-1], 1000)
y_fit = fitGaussian(x_fit, *popt)
plt.plot(x_fit, y_fit, lw=4, color="r")
plt.xlim(0,300)
plt.ylim(0,1e6)
plt.show()
EDIT: (response to Reblochon Masque)

