简体   繁体   中英

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)

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.

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.

Use the following indexing without the extra [] around the second index.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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