fit_erf.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from scipy import special
  2. from scipy.optimize import curve_fit
  3. from vedo import np, settings, Marker
  4. from vedo.pyplot import plot
  5. settings.default_font = 'Calco'
  6. settings.remember_last_figure_format = True
  7. xdata = [230, 234, 240, 243, 246, 249, 252]
  8. ydata = [0, 0, 11, 62, 15, 21, 100]
  9. tdata = [100, 31, 34, 80, 21, 21, 100]
  10. yerrs = np.sqrt(ydata) /np.array(tdata) + 0.1
  11. ydata = np.array(ydata) /np.array(tdata)
  12. def func(x, a, x0):
  13. return (1 + special.erf(a*(x-x0))) / 2
  14. p0 = [1/25, 240] # initial guess
  15. popt, _ = curve_fit(func, xdata, ydata, p0)
  16. x = np.linspace(225, 255, 50)
  17. y = func(x, *popt)
  18. x0, y0 = popt[1], func(popt[1], *popt)
  19. fig = plot(
  20. xdata,
  21. ydata,
  22. 'o',
  23. yerrors=yerrs,
  24. ylim=(-0.1,1.3),
  25. title="ERF(x) fit to data",
  26. ytitle='Embryos with visible HL',
  27. xtitle='Hind Limb age (h)',
  28. mc='blue2',
  29. ms=0.3,
  30. lwe=2,
  31. label='data',
  32. )
  33. fig += plot(x, y, lw=5, label='fit')
  34. fig += Marker('*', s=0.05, c='r4').pos(x0,y0, 0.1)
  35. fig.add_label(':mu', marker='*', mc="r4")
  36. fig.add_legend("top-left", vspace=2.5)
  37. fig.show(size=(900, 650), zoom='tight').close()