正如Joe Kington在回答这个问题时所解释的那样:How can I make a scatter plot colored by density in matplotlib,我制作了一个按密度着色的散点图。但是,由于我的数据分布复杂,我想更改用于计算密度的参数。以下是一些类似于我的假数据的结果:

我想校准gaussian_kde的密度计算,使图的左侧看起来像这样:

我不喜欢第一个图,因为点的组会影响相邻点组的密度,这会阻止我分析组内的分布。换句话说,即使这8组中的每一组都有完全相同的分布,在图表上也看不到。
我试图修改covariance_factor (就像我曾经对x上密度的2d图所做的那样),但是当gaussian_kde与多维数组一起使用时,它返回一个numpy.ndarray,而不是一个"scipy.stats.kde.gaussian_kde“对象。另外,我甚至不知道更改covariance_factor是否会起作用。
下面是我的虚拟代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
# Generate fake data
a = np.random.normal(size=1000)
b = np.random.normal(size=1000)
# Data for the first image
x = np.concatenate((a+10,a+10,a+20,a+20,a+30,a+30,a+40,a+40,a+80))
y = np.concatenate((b+10,b-10,b+10,b-10,b+10,b-10,b+10,b-10,b*4))
# Data for the second image
#x = np.concatenate((a+10,a+10,a+20,a+20,a+30,a+30,a+40,a+40))
#y = np.concatenate((b+10,b-10,b+10,b-10,b+10,b-10,b+10,b-10))
# Calculate the point density
xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)
# My unsuccesfull try to modify covariance which would work in 1D with "z = gaussian_kde(x)"
#z.covariance_factor = lambda : 0.01
#z._compute_covariance()
# Sort the points by density, so that the densest points are plotted last
idx = z.argsort()
x, y, z = x[idx], y[idx], z[idx]
fig, ax = plt.subplots()
ax.scatter(x, y, c=z, s=50, edgecolor='')
plt.show()解决方案可以使用其他密度计算器,我不介意。我们的目标是制作一个像上面显示的那样的密度图,在那里我可以玩密度参数。
我使用的是python 3.4.3
发布于 2016-07-17 19:16:48
有没有看过Seaborn?它不完全是您所要求的,但它已经具有生成密度图的功能:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import kendalltau
import seaborn as sns
# Generate fake data
a = np.random.normal(size=1000)
b = np.random.normal(size=1000)
# Data for the first image
x = np.concatenate((a+10, a+10, a+20, a+20, a+30, a+30, a+40, a+40, a+80))
y = np.concatenate((b+10, b-10, b+10, b-10, b+10, b-10, b+10, b-10, b*4))
sns.jointplot(x, y, kind="hex", stat_func=kendalltau)
sns.jointplot(x, y, kind="kde", stat_func=kendalltau)
plt.show()它提供了:

和

https://stackoverflow.com/questions/38379327
复制相似问题