Reputation: 1536
I have a set of astronomical data, to which I'm trying to fit a curve:
My fitting code is
param = stats.norm.fit(df['delta z'].dropna()) # Fit a normal distribution to the data
pdf_fitted = stats.norm.pdf(df['delta z'], *param)
x = np.linspace(*df['delta z'].agg([min, max]), 1000) # x-values
binwidth = np.diff(edges).mean()
ax.plot(x, stats.norm.pdf(x, *param)*h.sum()*binwidth, color = 'r')
which produces
Now, I'm clearly doing this in the wrong way, because the curve doesn't fit the data at all. All of the tutorials I've seen, such as here involve making a set of data, in which case we already know things like the mean and the skew. This question led me to estimate the parameters with
a_estimate, loc_estimate, scale_estimate = stats.skewnorm.fit(df['delta z'])
ax.plot(x, skewnorm.pdf(x, a_estimate, loc_estimate, scale_estimate), 'r-', lw=5, alpha=0.6, label='skewnorm pdf')
which produces
so how can I plot the fit with those parameters?
Upvotes: 2
Views: 190
Reputation: 4657
In the comments you state that you don't know how to plot the curve: here is a small example fitting and plotting skewnorm.
import numpy as np
import scipy.stats as ss
import matplotlib.pyplot as plt
data = ss. expon.rvs(size=1000)
P = ss.expon.fit(data)
rX = np.linspace(min(data), max(data), 50)
rP = ss.skewnorm.pdf(rX, *P)
plt.hist(data,bins=25, normed=True, color='slategrey')
plt.plot(rX, rP, color='darkturquoise')
plt.show()
Upvotes: 1