title: Scipy Curve_fit: Why is my fitting so poor and how to improve it?
body: <p>I am trying to fit my data using curve_fit library of python. Although I can capture the pattern of the data the real fit is rather poor. Is there any way I can improve the quality of the fit?</p>
  
  <p><a href="https://i.stack.imgur.com/FcN3b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FcN3b.png" alt="enter image description here"></a></p>
  
  <p>Here is my code:</p>
  
  <pre><code>import numpy as np
  from scipy.optimize import curve_fit
  from matplotlib import pyplot as plt
  
  x=np.array([ 4.29977288,  4.18759576,  3.937875  ,  3.68784896,  3.43711213,
      3.19099287,  2.94166468,  2.68543877,  2.4289324 ,  2.19035861,
      1.93962193,  1.69285434,  1.44271633,  1.18869615,  0.94761142,
      0.69828307,  0.44606364,  0.19355101, -0.05367106, -0.30303661,
     -0.55272018, -0.79877747, -1.04806864, -1.29706657, -1.54567223,
     -1.79685098, -2.05011095, -2.29874144, -2.54813208, -2.80178461,
     -3.04828379, -3.29893363, -3.54727073, -3.79908534, -4.04661293]);
  
  y=np.array([ 20.8744534 ,  20.89824536,  20.3763843 ,  19.79924837,
      19.19485964,  18.57716717,  17.93772371,  17.28834168,
      16.62367817,  15.94336213,  15.24389099,  14.52471466,
      13.7787734 ,  13.00299723,  12.18721413,  11.31510566,
      10.36672642,   9.32224105,   8.14237084,   6.78034367,
       5.19700447,   3.32945537,   1.10437136,  -1.48805508,
      -4.25695201,  -6.94906329,  -9.41648974, -11.54747381,
     -13.33444597, -14.90663076, -16.36783375, -17.72241553,
     -18.9592222 , -20.06703821, -21.07669491])
  
  def func(x,A,B,C):
      a=1+B/A
      b=1-B/A
      k=C/np.log(a/b)
      y=A*np.tanh((x-C)/(2*k))
      return y
  
  A_0=25
  B_0=10
  C_0=1.2
  
  
  popt,pcov = curve_fit(func,x,y,p0=[A_0,B_0,C_0])
  print(pcov)
  
  plt.plot(x,y,label='Data')
  plt.plot(x,func(x, *popt),'.',label='Fit')
  plt.legend()
  plt.show()
  </code></pre>