0

Using the following code, I can plot a histogram of my data and the fitting curve to the histogram.

mybins = len(data)//20
fig, (ax) = plt.subplots(1,1, figsize=(5, 3))
(mu,sigma) = scipy.stats.norm.fit(data)
n,bins,patches=ax.hist(data, bins=mybins, density=True, color='green', histtype='stepfilled', linewidth=1) 
ax.axvline(x=data.mean(), color='green', linestyle='dashed', linewidth=3)
y = scipy.stats.norm.pdf(bins, mu, sigma)
ax.plot(bins,y,'r--',color='green', linewidth=3)
ax.set_xlim(0.5,1.1)

This is the output which is correct fot the histogram and its vertical line of mean.
enter image description here

But, the fitting curve is a normal fitting against the mean.
I need help to implement a Weibull fit which should be resulting in a skewed fitting curve according to the data distribution.

Tried using the following from this:

shape, loc, scale = weibull_min.fit(data, floc=0)
plt.plot(data, stats.exponweib.pdf(data, *stats.exponweib.fit(data, 1, 1, scale=scale, loc=loc)))
plt.hist(data, label='PR', density=True, bins=len(data)//15, color = 'orangered')
plt.axvline(x=data.mean(), color='black', linestyle='dashed', linewidth=3)
plt.show()

And the result is
enter image description here

Edit:
Another try with sorted data:

fig, (ax) = plt.subplots(1,1, figsize=(5, 3))
ax.hist(data, bins=len(data)//15, color = 'orangered')
shape, loc, scale = weibull_min.fit(data, floc=0)
plt.plot(data, stats.exponweib.pdf(data, *stats.exponweib.fit(data, 1, 1, scale=scale, loc=loc)))
plt.show()

The result is now much better, but still not yet what it should be, i.e. the fit curve doesn't fit the 'size' of histogram.
enter image description here

My data can be downloaded from here.
Thanks for any help.

k.ko3n
  • 954
  • 8
  • 26
  • 1
    How about using [`weibull_min`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.weibull_min.html) from scipy's stats package – SpghttCd May 13 '19 at 15:52
  • Still wrong result here – k.ko3n May 13 '19 at 17:11
  • 2
    What you're showing is an effect of plotting unsorted data as a line plot. Sort the `data` object and everything will be fine. – merv May 13 '19 at 21:54
  • @merv suggestion about sorting data gives a much better result, but still not yet as it should be. Please see edit. – k.ko3n May 14 '19 at 09:12
  • 1
    The histogram plot should include the argument `normed=True`. – merv May 14 '19 at 09:22

0 Answers0