Seaborn clustermap with two row_colors

Viewed 871

Quick question, I have a clustermap with variable 'age_range' in row_colors and I would like to add the variable 'education' as a row_color as well. I have the following working code:

agerange = df_cor_small.pop("agerange")
lut = dict(zip(agerange.unique(), "rbg"))
row_colors = agerange.map(lut)

ax = sns.clustermap(df_cor_small, cmap='YlGnBu', row_colors=row_colors, figsize=(15,100), cbar_pos=(1.05, .2, .03, .4))

outputting this figure: enter image description here

(At the moment df_cor_small does not include the variable 'education' but it will once I know how to implement it, so it will be pop-able just like 'agerange')

Any suggestions how I could implement this?

1 Answers

You can provide the colors as a data.frame, I think you are providing a list right now. In the example below, I assign black to the 8 colors, 7x20 and 10 to the rows. Did not cluster the rows to show that the assignment is correct:

import seaborn as sns; sns.set(color_codes=True)
import string
iris = sns.load_dataset("iris")
species = iris.pop("species")
lut = dict(zip(species.unique(), "rbg"))
samples = np.repeat(list(string.ascii_letters[0:8]),20)[:150]
sample_cols = dict(zip(set(samples), sns.color_palette("cubehelix", 8)))

row_colors = pd.DataFrame({'species':species.map(lut),
                          'sample':[sample_cols[i] for i in samples]})
g = sns.clustermap(iris, row_colors=row_colors,row_cluster=False)

enter image description here

Related