반응형

seaborn 에서 clustermap에 color label 입히기.

condition_labels = df_data.condition
condition_uniq = sorted(condition_labels.unique())
condition_pal = sns.color_palette('husl',len(condition_uniq))
condition_lut = dict(zip(map(str, condition_uniq), condition_pal))
condition_colors = pd.Series(condition_labels).map(condition_lut)

tnm_labels = df_data.tnm
tnm_uniq = sorted(tnm_labels.unique())
tnm_pal = sns.color_palette('Paired',len(tnm_uniq))
tnm_lut = dict(zip(map(str, tnm_uniq), tnm_pal))
tnm_lut['NA']=(1,1,1)
tnm_colors = pd.Series(tnm_labels).map(tnm_lut)

age_labels = df_data.age
age_uniq = sorted(age_labels.unique())
age_pal = sns.color_palette('flare',len(age_uniq))
age_lut = dict(zip(map(str, sorted(age_labels.unique())), age_pal))
age_lut['NA']=(1,1,1)
age_colors = pd.Series(age_labels).map(age_lut)

condition_node_colors = pd.DataFrame(condition_colors).join(pd.DataFrame(tnm_colors)).join(pd.DataFrame(age_colors))
plt.figure(figsize=(100,120))
g = sns.clustermap(df_data.iloc[:,3:].T, cmap="vlag", col_colors = condition_node_colors)

for label in tnm_uniq:
    g.ax_col_dendrogram.bar(0, 0, color=tnm_lut[label], label=label, linewidth=10)
l2 = g.ax_col_dendrogram.legend(title='tnm', loc='center', ncol=2, bbox_to_anchor=(0.65, 1.05), bbox_transform=gcf().transFigure)
xx = []
for label in condition_uniq:
    x = g.ax_col_dendrogram.bar(0, 0, color=condition_lut[label], label=label, linewidth=10)
    xx.append(x)
#l1 = g.ax_col_dendrogram.legend(title='condition', loc='center', ncol=2, bbox_to_anchor=(0.2, 1.05), bbox_transform=gcf().transFigure)
legend3 = plt.legend(xx, condition_uniq, loc='center', title='condition', ncol=2, bbox_to_anchor=(0.35, 1.05), bbox_transform=gcf().transFigure)
yy = []
for label in age_uniq:
    y = g.ax_col_dendrogram.bar(0, 0, color=age_lut[label], label=label, linewidth=10)
    yy.append(y)
#l1 = g.ax_col_dendrogram.legend(title='condition', loc='center', ncol=2, bbox_to_anchor=(0.2, 1.05), bbox_transform=gcf().transFigure)
legend4 = plt.legend(yy, age_uniq, loc='center', title='age', ncol=5, bbox_to_anchor=(0.5, 1.15), bbox_transform=gcf().transFigure)

 

반응형

'Computer Science > python' 카테고리의 다른 글

프로토콜과 'abc' 모듈  (0) 2024.06.11
Python 데코레이터  (0) 2024.05.21
flask_sqlalchemy  (0) 2022.05.23
python 설치  (0) 2022.04.06
Progress bar 모듈 tqdm  (0) 2022.03.07

+ Recent posts