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]