Is it possible to generate a function z = f(x,y) where z is the density of points at x,y?

Viewed 118

I have some data and I want to create a function z=f(x,y) that approximates the density of points.

For example:

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)

enter image description here

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)

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

Is it possible to do it? Is it some sort of interpolation of the data?

0 Answers
Related