Something similar might have been posted but I am unable to fit a Gaussian to my data. It only produces a straight horizontal line. I tried the code with some randomly generated data and it works. I am not sure why it does not work with the actual data. I hope someone would be able to help me with this. Thank you.
import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt
# Data
y = np.array([395.27, 399.77, 436.10, 486.60, 561.20, 636.37, 784.90, 917.50, 965.53, 910.87, 897.67, 868.17, 762.93, 647.33, 519.37, 426.73, 375.87])
x = np.array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
# Find mean and sd
mu, std = norm.fit(y)
# plot original data
plt.scatter(x,y)
# plot Gaussian Fit
xp = np.linspace(0, len(x), 100)
p = norm.pdf(xp, mu, std)
plt.plot(xp, p, linewidth=2)
title = "Fit results: mu = %.2f, std = %.2f" % (mu, std)
plt.title(title)
plt.show()