3

I've been trying to fit some data I have gained from some simulations. From the curve, I guess a logarithmic fit would be ideal. However, the curve comes looking out quite funky. I've also tried higher order polynomials and np.polyfit, but I couldn't get either to work. Any help would be appreciated!

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


xdata=[9.24104360013e-06, 4.72619458107e-06, 4.03957328857e-06, 9.78301182748e-06, 1.36994566431e-05, 1.16294573409e-05, 7.70899546232e-06, 2.72587766232e-06, 2.19089955631e-06, 5.34851640035e-06, 7.84434545123e-06, 7.6524185787e-06, 1.00592536363e-05, 6.08711035578e-07, 4.08259572135e-07, 5.74424798328e-07, 6.20036326494e-07, 4.34755225756e-06, 4.72832211908e-06, 1.25156011417e-06, 1.44996714816e-05, 3.79992166335e-06, 4.45935911838e-06, 6.6307841155e-06, 2.38540191336e-06, 9.4649801666e-07, 9.11518608157e-06, 3.1944675219e-06, 5.32674287313e-06, 1.48463901861e-05, 3.41127723277e-06, 3.40027150288e-06, 3.33064781566e-06, 2.12828505238e-06, 7.22565690506e-06, 7.86527964811e-06, 2.25791582571e-06, 1.94875869207e-05, 1.54712884424e-05, 5.82300791075e-06, 9.5783833758e-06, 1.89519143607e-05, 1.03731970283e-05, 2.53090894753e-05, 9.26047056658e-06, 1.05428610146e-05, 2.89162870493e-05, 4.78624726782e-05, 1.00005855557e-05, 6.88617910928e-05]

ydata=[0.00281616449359, 0.00257023004939, 0.00250030932407, 0.00284317789756, 0.00300158447316, 0.00291690879783, 0.00274898865728, 0.0023625485679, 0.0023018015629, 0.00259860025555, 0.00269155777824, 0.00265941197135, 0.0028073724168, 0.00192920496041, 0.00182900945464, 0.00191452746379, 0.00193227563253, 0.00253266811688, 0.00255961306471, 0.00212426145702, 0.00285906942634, 0.00247877245272, 0.0025348504727, 0.00269881922057, 0.00232270371493, 0.00204672286703, 0.00281306442303, 0.00241938445736, 0.00261083321385, 0.00287440363274, 0.00244324770882, 0.00244364989768, 0.00244593671433, 0.00228714406931, 0.00263301289418, 0.00269385915315, 0.0022968948347, 0.00313898537645, 0.00305650121575, 0.00265291893623, 0.00278748794063, 0.00312801724905, 0.00289450806538, 0.00313176225397, 0.00284010926578, 0.0028957865422, 0.00335438183977, 0.00360421739757, 0.00270734995952, 0.00377301191882]


plt.plot(xdata,ydata,'o')

x = np.array(xdata, dtype=float) #transform your data in a numpy array of floats 
y = np.array(ydata, dtype=float) #so the curve_fit can work

#def func(x,a,b,c):
#    return a*x**2+ b*x +c

def func(x,a,b):
    return a*np.log(x)+ b


popt, pcov = curve_fit(func, x, y)
plt.plot(x, func(x, *popt), label="Fitted Curve")
plt.show()

Plot

3
  • 3
    Possible duplicate of scipy.curve_fit() returns multiple lines Commented Apr 29, 2019 at 13:46
  • 1
    If the data is logarithmic, you can compute the exponential and try a linear/polynomial fit. Commented Apr 29, 2019 at 13:51
  • You just need to sort your data while plotting the line. Use plt.plot(sorted(x), func(sorted(x), *popt), label="Fitted Curve"); plt.legend(). Commented Apr 29, 2019 at 13:55

1 Answer 1

2

Sort x before plotting

x_sorted = np.sort(x)
plt.plot(x_sorted, func(x_sorted, *popt), label="Fitted Curve")
plt.show()
Sign up to request clarification or add additional context in comments.

3 Comments

You can also just use plt.plot(sorted(x), func(sorted(x), *popt), label="Fitted Curve")
@Sheldore sorted is a python standard function, and thus is one order of magntitude slower than np.sort (at least on my machine). In addition, it returns a list, which you'd need to convert back to an ndarray again, if you process it further (does not matter just for plotting). Finally, you are calling the sorting algorithm twice, which is less efficient (does not matter in this case, either). Generally, when working with ndarrays, one should try to stick to pure numpy functions as much as possible.
Right, at least for such small list doesn’t make a difference though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.