是否可以生成一个函数 z = f(x,y),其中 z 是 x,y 点的密度?

问题描述

我有一些数据,我想创建一个函数 z=f(x,y) 来近似点的密度。

例如:

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

这里 Z 用于绘制颜色(绿色),但我想要该区域中该值的实际 Z 近似值(理想情况下是连续值而不是网格)

我的实际目标是一个类似于这里的密度图的函数,它给我一个 Z 值,无论该范围内的 X 或 Y 是什么,而不是绘图

能做到吗?这是数据的某种插值吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)