Reputation: 13
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.
Reproducible example:
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()
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.
Upvotes: 0
Views: 1787
Reputation: 13
for those who are interested. It should be following:
sns.clustermap(data, col_colors=col_colors.values,
col_cluster=False, row_cluster=False)
Note from a developer:
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.
Upvotes: 1