title: Gaussian summation for 2D scatter plots using python
body: <p>I am trying to establish what people would loosely refer to as a homemade KDE - I suppose. I am trying to evaluate a density of a rather huge set of datapoints. In particular, having many data points for a scatter, I want to indicate the density using a color gradient (see link below).</p>
  
  <p>For exemplification, I provide a random pair of (x,y) data below. The real data will be spread on different scales, hence the difference in X and Y grid point spacing.</p>
  
  <pre><code>import numpy as np
  from matplotlib import pyplot as plt
  
  def homemadeKDE(x, xgrid, y, ygrid, sigmaX = 1, sigmaY = 1):
  
      a = np.exp( -((xgrid[:,None]-x)/(2*sigmaX))**2 )
      b = np.exp( -((ygrid[:,None]-y)/(2*sigmaY))**2 ) 
  
      xweights = np.dot(a, x.T)/np.sum(a)
      yweights = np.dot(b, y.T)/np.sum(b)  
  
      return xweights, yweights
  
  x = np.random.rand(10000)
  x.sort()
  y = np.random.rand(10000)
  
  xGrid = np.linspace(0, 500, 501)
  yGrid = np.linspace(0, 10, 11)
  
  newX, newY = homemadeKDE(x, xGrid, y, yGrid)
  </code></pre>
  
  <p>What I am stuck with is, how to project these values back to the original x and y vector so I can use it for plotting a 2D scatter plot (x,y) with a z value for the density colored by a given color map like so:</p>
  
  <pre><code>plt.scatter(x, y, c = z, cmap = "jet")
  </code></pre>
  
  <p>Plotting and KDE approach is in fact inspired by this great <a href="https://stackoverflow.com/a/20107592/3722211%20">answer</a></p>
  
  <p><strong>EDIT 1</strong>
  To smooth out some confusion, the idea is to do a gaussian KDE, which would be on a much coarser grid. SigmaX and sigmaY reflect the bandwidth of the kernel in x and y directions, respectively.</p>