additional row colors in seaborn cluster map

前端 未结 3 749
遥遥无期
遥遥无期 2021-02-06 09:17

I am currently generating clustermaps in seaborn and labeling the row colors as below.

matrix = pd.DataFrame(np.random.random_integers(0,1, size=(50,4)))
labels          


        
3条回答
  •  盖世英雄少女心
    2021-02-06 09:58

    There is another option for feeding in the annotation colors: you can provide a whole dataframe in the row colors or col_colors options, instead of a list of lists.

    This strategy might be particularly helpful if you have a dataframe with several annotations you want represented. Instead of map, you can use the pandas function replace.

    Something such as this bit can be used to modify the other answer:

    ## This step is necessary because you can't use replace with the tuple rgb values
    lut = {k:matplotlib.colors.to_hex(v) for k, v in lut.iteritems()}
    
    annotations_df = annotations_df.replace(lut)
    
    g=sns.clustermap(matrix, col_cluster=False, linewidths=0.1, cmap='coolwarm', row_colors=annotations_df)
    plt.show()
    

提交回复
热议问题