Python seaborn的clustermap中的列颜色给出了意外的结果 [英] Column colors in clustermap of Python seaborn give unexpected results

查看:289
本文介绍了Python seaborn的clustermap中的列颜色给出了意外的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试可视化具有不同颜色的列簇.但是,就我而言,颜色并没有真正显示标签.不知道出什么问题了.

I am trying to visualize clusters of columns with different colors. However, in my case colors do not really show labels. Not sure what is wrong.

可复制的示例:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

data = pd.DataFrame(np.random.normal(0, 1, [100, 100]))
labels = np.repeat([0, 1, 2, 3], [25, 25, 25, 25], axis=0)
data.columns = labels
lut = dict(zip(set(labels), sns.mpl_palette("autumn", len(set(labels)))))
col_colors=pd.DataFrame(labels)[0].map(lut)

sns.clustermap(data, col_colors=col_colors,
               col_cluster=False, row_cluster=False)
plt.show()

我们可以看到col_colors具有4个唯一值:

We can see that col_colors have 4 unique values:

print(len(set(col_colors)))

我们可以看到列代表不同的标签(图的底部).但是,在我的情况下,列仅以红色表示.

And we can see that columns represent different labels (bottom of the plot). However, in my case columns are represented in only red color.

它在我的桌面上的外观

推荐答案

面向有兴趣的人.应该是:

for those who are interested. It should be following:

sns.clustermap(data, col_colors=col_colors.values,
           col_cluster=False, row_cluster=False)

开发者的注意事项:

当col_colors是Pandas对象时,索引信息用于将它们与列对齐(与行相同).这意味着在您的情况下,仅使用前三种颜色,在您的示例中全部为红色.如果您不想在索引上进行匹配,请传递col_colors.values.

When the col_colors are a Pandas object, the index information is used to align them with the columns (same for the rows). That means that in your case, only the first three colors are used, which in your example are all red. Pass col_colors.values if you don't want to match on the index.

这篇关于Python seaborn的clustermap中的列颜色给出了意外的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆