Shape issue in my python code related to fitting using the power law

57 Views Asked by At

I tried to fit the flux of cosmic rays versus energy for AllParticles&H. I used the CRDB package to extract the data. Then, i used the power law to fit. However, the shape of x is (1236,) and the error in y is (1236,2). how do i fix the shape issue?

Here is my code:

 x = t_combined.e 
y = t_combined.value 
err = t_combined.err_sta
    lsq = LeastSquares(x, y, err, power_law) 
m = Minuit(lsq, a=1, gamma=-2.0)
    plt.errorbar(x, y, err, fmt="o", label="data") 
plt.plot(x, (x, *m.values), label="fit") # what does this line do?
    ax.scatter(x, y, label="Combined original data", marker="x")
    a_fit = minuit.values.a gamma_fit = minuit.values.gamma
    x_fit = np.logspace(np.log10(t_combined['e'].min()), np.log10(t_combined['e'].max()), 100) y_fit = power_law(x_fit, a_fit, gamma_fit) 
ax.plot(x_fit, y_fit, label="Fitted power law", linestyle='--', color='red')
    plt.xlabel(r" [GeV]") 
plt.ylabel(r" d/d [1/(m2 s sr)]") 
plt.title('Power Law Fit') 
plt.legend() 
plt.xscale('log')
    plt.yscale('log') 
plt.show()
    print("Fitted parameters (a, gamma):", m)
    plt.show()
0

There are 0 best solutions below