0

I am wondering if there is a method to fit a curve to a bar chart. In the example below, I have coefficients for each class, and it is clear that the data follows a distribution. However, because the data is categorical, I am unsure if there is a way to fit a normal distribution or another type of distribution that would capture the envelope of the function that has the best fit with our bar chart. enter image description here

I have try this but I'm not such if it is corret or not :

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit


# "x" is data, a,b,c are parameters
def func(x, a, b, c):
   return a * np.exp(-(x-b)**2/(2*c**2)) # example function


xData = np.array([1,2,3,4,5,6,7])
yData = np.array(data.tolist())

# curve fit the data using curve_fit's default inital parameter estimates
fittedParameters, pcov = curve_fit(func, xData, yData)

y_fit = func(xData, *fittedParameters)

plt.bar(xData, yData) # plot the raw data as bar chart
plt.plot(xData, y_fit) # plot the equation using the fitted parameters
plt.show()

print(fittedParameters)```

0 Answers0