简体   繁体   English

如何制作大型2D散点图的轮廓/密度图

[英]How to make a contour/density plot of a large 2D scatter plot

I have an overcrowded scatter plot and I'm trying to create a contour or density plot to see if there is any distinct populations in my data. 我有一个过于拥挤的散点图,我试图创建一个等高线图或密度图,以查看我的数据中是否有任何不同的总体。 I have tried the following code but I get the error: 我尝试了以下代码,但出现错误:

too many values to unpack (expected 2) 太多值无法解包(预期2)

My code is: 我的代码是:

x = CDM_300[:,[1]]
y = CDM_300[:,[2]]

# Evaluate a gaussian kde on a regular grid of nbins x nbins over data extents
nbins=300
k = kde.gaussian_kde([x,y])
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))

# Make the plot
plt.pcolormesh(xi, yi, zi.reshape(xi.shape))
plt.show()

# Change color palette
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.Greens_r)
plt.show()

CDM_300 is a (23800, 3) array, if I try to np.meshgrid the data, my laptop just crashes. CDM_300是一个( CDM_300 )数组,如果我尝试对数据进行np.meshgrid ,我的笔记本电脑将崩溃。

The problem seems to arise from the way you have indexed the data. 问题似乎是由您为数据建立索引的方式引起的。 When you do [:, [1]] , the shape of your data becomes (23800, 1) and each element is an array in itself. 当您执行[:, [1]] ,数据的形状变为(23800, 1) ,每个元素本身就是一个数组。

Use the following indexing without the extra [] around the second index. 使用以下索引,第二个索引周围没有多余的[]

x = CDM_300[:, 1]
y = CDM_300[:, 2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM