title: Fitting a 2D Gaussian function using scipy.optimize.curve_fit - ValueError and minpack.error
body: <p>I intend to fit a 2D Gaussian function to images showing a laser beam to get its parameters like <code>FWHM</code> and position. So far I tried to understand how to define a 2D Gaussian function in Python and how to pass x and y variables to it.</p>
  
  <p>I've written a little script which defines that function, plots it, adds some noise to it and then tries to fit it using <code>curve_fit</code>. Everything seems to work except the last step in which I try to fit my model function to the noisy data. Here is my code:</p>
  
  <pre><code>import scipy.optimize as opt
  import numpy as np
  import pylab as plt
  
  
  #define model function and pass independant variables x and y as a list
  def twoD_Gaussian((x,y), amplitude, xo, yo, sigma_x, sigma_y, theta, offset):
      xo = float(xo)
      yo = float(yo)    
      a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)
      b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)
      c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)
      return offset + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo) + c*((y-yo)**2)))
  
  # Create x and y indices
  x = np.linspace(0, 200, 201)
  y = np.linspace(0, 200, 201)
  x,y = np.meshgrid(x, y)
  
  #create data
  data = twoD_Gaussian((x, y), 3, 100, 100, 20, 40, 0, 10)
  
  # plot twoD_Gaussian data generated above
  plt.figure()
  plt.imshow(data)
  plt.colorbar()
  
  # add some noise to the data and try to fit the data generated beforehand
  initial_guess = (3,100,100,20,40,0,10)
  
  data_noisy = data + 0.2*np.random.normal(size=len(x))
  
  popt, pcov = opt.curve_fit(twoD_Gaussian, (x,y), data_noisy, p0 = initial_guess)
  </code></pre>
  
  <p>Here is the error message I get when running the script using <code>winpython 64-bit</code> <code>Python 2.7</code>:</p>
  
  <pre><code>ValueError: object too deep for desired array
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "C:\Python\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
      execfile(filename, namespace)
    File "E:/Work Computer/Software/Python/Fitting scripts/2D Gaussian function fit/2D_Gaussian_LevMarq_v2.py", line 39, in &lt;module&gt;
      popt, pcov = opt.curve_fit(twoD_Gaussian, (x,y), data_noisy, p0 = initial_guess)
    File "C:\Python\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\scipy\optimize\minpack.py", line 533, in curve_fit
      res = leastsq(func, p0, args=args, full_output=1, **kw)
    File "C:\Python\WinPython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\scipy\optimize\minpack.py", line 378, in leastsq
      gtol, maxfev, epsfcn, factor, diag)
  minpack.error: Result from function call is not a proper array of floats.
  </code></pre>
  
  <p>What is it that am I doing wrong? Is it how I pass the independent variables to the model <code>function/curve_fit</code>?</p>