Fit data in Python (sine wave?)
<p>I have data which I would like to visualise. Here is my code:</p>

<pre><code>import numpy as np 
from scipy.optimize 
import curve_fit

xdata = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])   
ydata = np.array([26.2, 27.2, 27.9, 27.9, 27.2, 26.2, 25.3, 24.5, 24.2, 24.4, 24.7, 25.4])

def func(x, p1,p2,p3):
    return p1 + p3*np.sin(p2*x)

popt, pcov = curve_fit(func, xdata, ydata,p0=(25.931, 0.5629,1.8978))  

import matplotlib.pylab as plt   
plt.plot(xdata, ydata,'o')   
plt.plot(xdata, func(xdata, popt[0], popt[1], popt[2]),linewidth=2, color = 'red')   
plt.show()
</code></pre>

<p>I am unhappy with the result. How can I get a better fit? Does the function I define need to be more complex? Do I need a completely different approach, another method? </p>

<p>I also tried this approach:</p>

<pre><code>from scipy.interpolate import interp1d  
f = interp1d(xdata, ydata, kind='cubic')  
xnew = np.linspace(0, 11, num=100, endpoint=True)  
plt.plot(xdata, ydata, 'o', xnew, f(xnew), '-', color = 'blue')
</code></pre>

<p>The result looks much nicer.
I am searching for a fitting method which gives a similar result when plotted. I am sorry for being unable to express it clearer. That's probably already part of the problem :(</p>