Reputation: 95
I plotted a graph for entropy. Now I want to do a curve fitting but I am not able to understand how to initiate the process. I tried it using the curve_fit
module of scipy
but what I get is just a straight line rather than a Gaussian curve. Here is the output:
from scipy.optimize import curve_fit
x = [18,23,95,142,154,156,157,158,258,318,367,382,484,501,522,574,681,832,943,1071,1078,1101,1133,1153,1174,1264]
y = [0.179,0.179,0.692,0.574,0.669,0.295,0.295,0.295,0.387,0.179,0.179,0.462,0.179,0.179,0.179,0.179,0.179,0.179,0.179,0.179,0.179,0.462,0.179,0.387,0.179,0.295]
x = np.asarray(x)
y = np.asarray(y)
def Gauss(x, A, B):
y = A*np.exp(-1*B*x**2)
return y
parameters_g, covariance_g = curve_fit(Gauss, x, y)
fit_A = parameters_g[0]
fit_B = parameters_g[1]
print(fit_A)
print(fit_B)
fit_y = Gauss(x, fit_A, fit_B)
plt.figure(figsize=(18,10))
plt.plot(x, y, 'o', label='data')
plt.plot(x, fit_y, '-', label='fit')
plt.legend()
plt.show()
I just connected the values using spline and got this kind of curve:
Can anyone suggest to me how to fit this curve into a Gaussian curve?
Edit 1: Now I have the barplot (sort of) where I have the y value for the corresponding x values. My x-axis ranges from 0 to 1273 and the y-axis from 0 to 1. How can I do a curve fitting and what will be the right curve over here? I was trying to fit a bimodal curve distribution for the given data. You can find the data from here.
Bar plot image: https://i.sstatic.net/1Awt5.png
Data : https://drive.google.com/file/d/1_uiweIWRWgzy5wNVLOvn4WN25jteu8rQ/view?usp=sharing
Upvotes: 1
Views: 327
Reputation: 827
You dont have a line you indeed have a Gaussian curve (centered around zero because of your definition of Gauss). You can clearly see that when you plot the function on a different scale:
x_arr = np.linspace(-2,2,100)
fit_y = Gauss(x_arr, fit_A, fit_B)
plt.figure(figsize=(18,10))
plt.plot(x_arr, fit_y, '-', label='fit')
plt.legend()
plt.show()
That image was made with the estimated fit_A == fit_B == 1
from your code.
You can add a different initial guess which leads to different result via:
parameters_g, covariance_g = curve_fit(Gauss, x, y, p0=(1,1/1000))
but I would say that your data is not that well described via a Gaussian curve.
One thing I would allays recommend is setting those values by hand to see the effect it has on the plot. That way you can get a feeling if the task you try to automated is realistic in the first place.
Upvotes: 1