pyplot.scatter 中基于值的随机颜色

发布于 2025-01-14 11:53:45 字数 849 浏览 2 评论 0原文

我有一个大的 pandas 数据框,我对其进行了聚类,并且集群 ID 存储在数据框的一列中。 我想以每个簇都有不同颜色的方式显示簇。 我尝试使用颜色图执行此操作,但问题是我有太多的点和簇,因此附近的簇仅分配略有不同的颜色,因此当我绘制所有这些时,我只会得到一个如下所示的大混搭:

在此处输入图像描述

注意该图像包含大约 4000 个簇,但由于簇的颜色只是从上到下分配的,因此附近的簇混合在一起。

我希望附近的簇被涂成不同的颜色,所以我尝试为每个簇制作随机颜色,然后根据每个点的簇标签为每个点分配一个颜色,如下所示:

# creating a color for each distinct cluster label
colors = [(random.random(), random.random(), random.random())
          for _ in range(len(set(data['labels'])))]
# assigning color to a point based on its cluster label
for index, row in data.iterrows():
    plt.scatter(row['x'], row['y'], color=colors[int(row['labels'])])

现在这个程序可以工作,但是很多 上面的矢量化版本较慢。

有没有一种方法可以在不编写 for 循环的情况下为每个簇着色明显不同的颜色?

I have a large pandas dataframe that I clustered and the cluster id is stored in a column of a dataframe.
I would like to display clusters in such a way that each cluster has a different color.
I tried doing this with a colormap but the problem is that I have too many points and clusters so nearby clusters get assign only slightly different colors, so when I plot all of them I just get a big mashup that looks like this:

enter image description here

Note that this is image contains about 4000 clusters, but because colors of clusters are just assigned top to bottom, nearby clusters blend together.

I would like nearby clusters to be painted in different colors so I tried making a random color for each cluster and then assign each point a color based on its cluster label like this:

# creating a color for each distinct cluster label
colors = [(random.random(), random.random(), random.random())
          for _ in range(len(set(data['labels'])))]
# assigning color to a point based on its cluster label
for index, row in data.iterrows():
    plt.scatter(row['x'], row['y'], color=colors[int(row['labels'])])

Now this program works but it is much slower that vectorized version above.

Is there a way to do color each cluster in clearly different colors without writing a for loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坠似风落 2025-01-21 11:53:45

这会创建一个 256 种颜色的随机颜色图,然后您可以将其传递给 scatter :

def segmentation_cmap():
    vals = np.linspace(0, 1, 256)
    np.random.shuffle(vals)
    return plt.cm.colors.ListedColormap(plt.cm.CMRmap(vals))


ax.scatter(row['x'],row['y'],c=row['labels'],s=1,cmap=segmentation_cmap())

您可以添加颜色,但在某些时候无论如何您都很难看到差异!

This creates a random colormap of 256 colors that you can then pass to scatter :

def segmentation_cmap():
    vals = np.linspace(0, 1, 256)
    np.random.shuffle(vals)
    return plt.cm.colors.ListedColormap(plt.cm.CMRmap(vals))


ax.scatter(row['x'],row['y'],c=row['labels'],s=1,cmap=segmentation_cmap())

You may add colors, but you would have trouble seeing the differences anyways at some point !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文