pyplot.scatter 中基于值的随机颜色
我有一个大的 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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会创建一个 256 种颜色的随机颜色图,然后您可以将其传递给 scatter :
您可以添加颜色,但在某些时候无论如何您都很难看到差异!
This creates a random colormap of 256 colors that you can then pass to scatter :
You may add colors, but you would have trouble seeing the differences anyways at some point !