当前位置:网站首页>Adding color blocks to Seaborn clustermap matrix

Adding color blocks to Seaborn clustermap matrix

2022-07-01 08:09:00 qq_ forty-five million seven hundred and fifty-nine thousand tw

Use cases 1

import seaborn as sns; sns.set(color_codes=True)
import matplotlib.pyplot as plt
import pandas as pd

iris = sns.load_dataset("iris")
print(iris)
species = iris.pop("species")


lut1 = dict(zip(species.unique(), ['#ED2323','#60FD00','#808080']))
row_colors1 = species.map(lut1)

lut2 = dict(zip(species.unique(), "rbg"))
row_colors2 = species.map(lut2)

row_colors = pd.concat([row_colors1,row_colors2],axis=1)
print(row_colors)

g = sns.clustermap(iris, row_colors=row_colors, col_cluster=False,cmap="mako", yticklabels=False, xticklabels=False)

plt.show()

give the result as follows
 Insert picture description here

Use cases 2( Custom color )

import seaborn as sns; sns.set_theme(color_codes=True)
iris = sns.load_dataset("iris")
species = iris.pop("species")
sample=20
iris=iris.head(20)
species=species.head(sample)
g = sns.clustermap(iris)

 Insert picture description here


colors_use=['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#bcbd22', '#17becf', '#aec7e8', '#ffbb78', '#98df8a', '#ff9896', '#bec1d4', '#bb7784', '#0000ff', '#111010', '#FFFF00',   '#1f77b4', '#800080', '#959595', 
 '#7d87b9', '#bec1d4', '#d6bcc0', '#bb7784', '#8e063b', '#4a6fe3', '#8595e1', '#b5bbe3', '#e6afb9', '#e07b91', '#d33f6a', '#11c638', '#8dd593', '#c6dec7', '#ead3c6', '#f0b98d', '#ef9708', '#0fcfc0', '#9cded6', '#d5eae7', '#f3e1eb', '#f6c4e1', '#f79cd4']

#lut = dict(zip(species.unique(), colors_use))
row_colors = colors_use[:sample]
g = sns.clustermap(iris, row_colors=row_colors)
##  I added a square here , The next one is easy 

 Insert picture description here

原网站

版权声明
本文为[qq_ forty-five million seven hundred and fifty-nine thousand tw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010805291769.html