title: Fit sigmoid function ("S" shape curve) to data using Python
body: <p>I'm trying to fit a sigmoid function to some data I have but I keep getting:<code>ValueError: Unable to determine number of fit parameters.</code></p>
  <p>My data looks like this:</p>
  <p><a href="https://i.stack.imgur.com/LEZjb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LEZjb.png" alt="enter image description here" /></a></p>
  <p>My code is:</p>
  <pre><code>from scipy.optimize import curve_fit
  
  def sigmoid(x):
      return (1/(1+np.exp(-x)))
  
  popt, pcov = curve_fit(sigmoid, xdata, ydata, method='dogbox')
  </code></pre>
  <p>Then I get:</p>
  <pre><code>---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-5-78540a3a23df&gt; in &lt;module&gt;
        2     return (1/(1+np.exp(-x)))
        3 
  ----&gt; 4 popt, pcov = curve_fit(sigmoid, xdata, ydata, method='dogbox')
  
  ~\Anaconda3\lib\site-packages\scipy\optimize\minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
      685         args, varargs, varkw, defaults = _getargspec(f)
      686         if len(args) &lt; 2:
  --&gt; 687             raise ValueError(&quot;Unable to determine number of fit parameters.&quot;)
      688         n = len(args) - 1
      689     else:
  
  ValueError: Unable to determine number of fit parameters.
  </code></pre>
  <p>I'm not sure why this does not work, it seems like a trivial action--&gt; fit a curve to some point. The desired curve would look like this:</p>
  <p><a href="https://i.stack.imgur.com/aQPB9.png" rel="noreferrer"><img src="https://i.stack.imgur.com/aQPB9.png" alt="enter image description here" /></a></p>
  <p>Sorry for the graphics.. I did it in PowerPoint...</p>
  <p>How can I find the best sigmoid (&quot;S&quot; shape) curve?</p>
  <p><strong>UPDATE</strong></p>
  <p>Thanks to @Brenlla I've changed my code to:</p>
  <pre><code>def sigmoid(k,x,x0):
      return (1 / (1 + np.exp(-k*(x-x0))))
  
  popt, pcov = curve_fit(sigmoid, xdata, ydata, method='dogbox')
  </code></pre>
  <p>Now I do not get an error, but the curve is not as desired:</p>
  <pre><code>x = np.linspace(0, 1600, 1000)
  y = sigmoid(x, *popt)
  
  plt.plot(xdata, ydata, 'o', label='data')
  plt.plot(x,y, label='fit')
  plt.ylim(0, 1.3)
  plt.legend(loc='best')
  </code></pre>
  <p>and the result is:</p>
  <p><a href="https://i.stack.imgur.com/YHhON.png" rel="noreferrer"><img src="https://i.stack.imgur.com/YHhON.png" alt="enter image description here" /></a></p>
  <p>How can I improve it so it will fit the data better?</p>
  <p><strong>UPDATE2</strong></p>
  <p>The code is now:</p>
  <pre><code>def sigmoid(x, L,x0, k, b):
      y = L / (1 + np.exp(-k*(x-x0)))+b
  </code></pre>
  <p>But the result is still...</p>
  <p><a href="https://i.stack.imgur.com/IPrpC.png" rel="noreferrer"><img src="https://i.stack.imgur.com/IPrpC.png" alt="enter image description here" /></a></p>