python - 用 hexbin 覆盖多组数据

标签 python matplotlib cluster-analysis scatter-plot seaborn

我正在对一个非常密集的大型数据集进行一些 KMeans 聚类,并且我正在尝试找出可视化聚类的最佳方法。

在 2D 中,它看起来像 hexbin会做得很好,但我无法在同一个数字上重叠绘制集群。我想用 hexbin在每个集群上分别使用不同的颜色图,但由于某种原因,这似乎不起作用。 该图显示了我尝试绘制第二组和第三组数据时得到的结果。

关于如何解决这个问题有什么建议吗? enter image description here

经过一些摆弄,我能够用 Seaborn's kdeplot 做到这一点

enter image description here

最佳答案

我个人认为 kdeplot 的解决方案非常好(尽管我会在集群拦截的部分做一些工作)。无论如何,作为对您问题的回应,您可以为 hexbin 提供最小计数(将所有空单元格保持透明)。这是一个小函数,可以为可能想要进行一些实验的任何人生成随机集群(在评论中,您的问题似乎引起了用户的极大兴趣,请随意使用它):

import numpy as np
import matplotlib.pyplot as plt

# Building random clusters
def cluster(number):
    def clusterAroundX(a,b,number):
        x = np.random.normal(size=(number,))
        return (x-x.min())*(b-a)/(x.max()-x.min())+a
    def clusterAroundY(x,m,b):
        y = x.copy()
        half   = (x.max()-x.min())/2
        middle = half+x.min()
        for i in range(x.shape[0]):
            std = (x.max()-x.min())/(2+10*(np.abs(middle-x[i])/half))
            y[i] = np.random.normal(x[i]*m+b,std)
        return y + np.abs(y.min())
    m,b = np.random.randint(-700,700)/100,np.random.randint(0,50)
    print(m,b)
    f = np.random.randint(0,30)
    l = f + np.random.randint(10,50)
    x = clusterAroundX(f,l,number)
    y = clusterAroundY(x,m,b)
    return x,y

,使用这段代码,我生成了一些聚类,并用散点图绘制了它们(我通常将其用于我自己的聚类分析,但我想我应该看看 seaborn)、hexbin、imshow(将 pcolormesh 更改为更多控制)和 contourf:

clusters = 5
samples  = 300
xs,ys = [],[]
for i in range(clusters):
    x,y = cluster(samples)
    xs.append(x)
    ys.append(y)

# SCATTERPLOT
alpha = 1
for i in range(clusters):
    x,y = xs[i],ys[i]
    color = (np.random.randint(0,255)/255,np.random.randint(0,255)/255,np.random.randint(0,255)/255)
    plt.scatter(x,y,c = color,s=90,alpha=alpha)
plt.show()

# HEXBIN
# Hexbin seems a bad choice because I think you cant control the size of the hexagons.
alpha = 1
cmaps = ['Reds','Blues','Purples','Oranges','Greys']
for i in range(clusters):
    x,y = xs[i],ys[i]
    plt.hexbin(x,y,gridsize=20,cmap=cmaps.pop(),mincnt=1)
plt.show()

# IMSHOW
alpha = 1
cmaps = ['Reds','Blues','Purples','Oranges','Greys']
xmin,xmax = min([i.min() for i in xs]), max([i.max() for i in xs])
ymin,ymax = min([i.min() for i in ys]), max([i.max() for i in ys])
nums = 30
xsize,ysize  = (xmax-xmin)/nums,(ymax-ymin)/nums
im = [np.zeros((nums+1,nums+1)) for i in range(len(xs))]
def addIm(im,x,y):
    for i,j in zip(x,y):
        im[i,j] = im[i,j]+1
    return im
for i in range(len(xs)):
    xo,yo = np.int_((xs[i]-xmin)/xsize),np.int_((ys[i]-ymin)/ysize)
    #im[i][xo,yo] = im[i][xo,yo]+1
    im[i] = addIm(im[i],xo,yo)
    im[i] = np.ma.masked_array(im[i],mask=(im[i]==0))
for i in range(clusters):
    # REPLACE BY pcolormesh if you need more control over image locations.
    plt.imshow(im[i].T,origin='lower',interpolation='nearest',cmap=cmaps.pop())
plt.show()

# CONTOURF
cmaps = ['Reds','Blues','Purples','Oranges','Greys']
for i in range(clusters):
    # REPLACE BY pcolormesh if you need more control over image locations.
    plt.contourf(im[i].T,origin='lower',interpolation='nearest',cmap=cmaps.pop())
plt.show()

,结果如下:

scatterplot clusters

hexbin clusters

imshow clusters

countourf clusters

关于python - 用 hexbin 覆盖多组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31523812/

相关文章:

python - 如何在 Pandas 中绘制 groupby 之后的聚合结果?

Python - 使用 matplotlib.pyplot 制作动画

algorithm - 聚类算法和 "extending"聚类以包括 N 个最近的邻居

python - 您如何根据 Python 中的相似性或重叠性来比较两个集群分组?

python - 无法让 scipy 层次聚类工作

python - 用平均值估算多列 NaN 值的最佳方法

python - 运行时错误 : No MovieWriters available in Matplotlib animation

python - 游标在 PyMySQL 中起什么作用和作用?

python - 从 cron 表达式创建 apscheduler 作业触发器

python - 返回一个赞美的核苷酸字符