Search code examples
pythonmachine-learningscipyweibull

Weibull Distribution Scipy


Following is the code I wrote for Weibull Distribution which generates data which will fit a Weibull distribution and try to plot the same

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt

data = stats.exponweib.rvs(a=1, c=2.09, scale=10.895, loc=0, size=2500)

plt.plot(data, stats.exponweib.pdf(data, *stats.exponweib.fit(data, 1, 1, scale=02, loc=0))

_ = plt.hist(data, bins = np.linspace(0, 16, 33), normed=True, alpha=0.5)

plt.show()

My Question :-

I want a single line interpolation over the bins, why I am getting messed up plot ?


Solution

  • The matplotlib plot function plots the curve connecting the points in the order in which the points are given. To get the curve that you expect, sort data before plotting it. E.g.:

    data = stats.exponweib.rvs(a=1, c=2.09, scale=10.895, loc=0, size=2500)
    data.sort()
    plt.plot(data, stats.exponweib.pdf(data, *stats.exponweib.fit(data, 1, 1, scale=2, loc=0)))