title: How can I fit multiple Gaussian curved to mass spectrometry data in Python?
body: <p>I would like to fit multiple Gaussian curves to Mass spectrometry data in Python. Right now I'm fitting the data one Gaussian at a time -- literally one range at a time. </p>
  
  <p>Is there a more streamlined way to do this? Is there a way I can run the data through a loop to plot a Gaussian at each peak? I'm guessing there's gotta be a better way, but I've combed through the internet.</p>
  
  <p>My graph for two Gaussians is shown below. </p>
  
  <p><img src="https://i.stack.imgur.com/zYW6y.png" alt="Mass Spectrometry py.plot with two Gaussian Fits"></p>
  
  <p>My example data can be found at: <a href="http://txt.do/dooxv" rel="nofollow noreferrer">http://txt.do/dooxv</a></p>
  
  <p>And here's my current code:</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import scipy.optimize as opt
  
  from scipy.interpolate import interp1d
  
  RGAdata = np.loadtxt("/Users/ilenemitchell/Desktop/RGAscan.txt", skiprows=14)
  RGAdata=RGAdata.transpose()
  
  x=RGAdata[0]
  y=RGAdata[1]
  
  # graph labels
  plt.ylabel('ion current')
  plt.xlabel('mass/charge ratio')
  plt.xticks(np.arange(min(RGAdata[0]), max(RGAdata[0])+2, 2.0))
  plt.ylim([10**-12.5, 10**-9])
  plt.title('RGA Data Jul 25, 2017')
  
  plt.semilogy(x, y,'b')
  
  #fitting a guassian to a peak
  
  def gauss(x, a, mu, sig):
  return a*np.exp(-(x-mu)**2/(2*sig**2))
  
  
  fitx=x[(x&gt;40)*(x&lt;43)]
  fity=y[(x&gt;40)*(x&lt;43)]
  mu=np.sum(fitx*fity)/np.sum(fity)
  sig=np.sqrt(np.sum(fity*(fitx-mu)**2)/np.sum(fity))
  
  print (mu, sig, max(fity))
  
  popt, pcov = opt.curve_fit(gauss, fitx, fity, p0=[max(fity),mu, sig])
  plt.semilogy(x, gauss(x, popt[0],popt[1],popt[2]), 'r-', label='fit')
  
  #second guassian
  
  fitx2=x[(x&gt;26)*(x&lt;31)]
  fity2=y[(x&gt;26)*(x&lt;31)]
  mu=np.sum(fitx2*fity2)/np.sum(fity2)
  sig=np.sqrt(np.sum(fity2*(fitx2-mu)**2)/np.sum(fity2))
  
  print (mu, sig, max(fity2))
  
  popt2, pcov2 = opt.curve_fit(gauss, fitx2, fity2, p0=[max(fity2),mu, sig])
  plt.semilogy(x, gauss(x, popt2[0],popt2[1],popt2[2]), 'm', label='fit2')
  
  plt.show()
  </code></pre>