title: Is it possible to generate a function z = f(x,y) where z is the density of points at x,y?
body: <p>I have some data and I want to create a function  <em>z=f(x,y)</em>  that approximates the density of points.</p>
  <p>For example:</p>
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from scipy.stats import kde
   
  # Create data: 200 points
  data = np.random.multivariate_normal([0, 0], [[1, 0.5], [0.5, 3]], 200)
  x, y = data.T
   
  # Create a figure with 2 plot areas
  fig, axes = plt.subplots(ncols=2, nrows=1, figsize=(21, 5))
   
  # Starts with a Scatterplot
  axes[0].set_title('Scatterplot')
  axes[0].plot(x, y, 'ko')
  
  # I can create a density plot with matlib
  
  # Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
  nbins = 20
  k = kde.gaussian_kde(data.T)
  xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
  zi = k(np.vstack([xi.flatten(), yi.flatten()]))
   
  # plot a density
  axes[1].set_title('Calculate Gaussian KDE')
  axes[1].pcolormesh(xi, yi, zi.reshape(xi.shape), shading='auto', cmap=plt.cm.BuGn_r)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/xTAOt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xTAOt.png" alt="enter image description here" /></a></p>
  <p>Here the Z is used to plot the color(green) but I want the actual Z approximation of that value in that area (ideally continuous value and not a grid)</p>
  <p><strong>My actual goal is a function that similarly to the density plot here gives me a Z value whatever is the X or Y within that range and not a Plot</strong></p>
  <p>Is it possible to do it? Is it some sort of interpolation of the data?</p>