我正在使用以下代码创建clustermap
import numpy as np
import pandas as pd
import seaborn as sns
all_net_names = ['early_vis', 'face', 'motion', 'scene', 'scene', 'scene',
'dmn', 'dmn', 'dmn', 'dmn', 'dmn', 'dmn', 'reward', 'reward',
'reward', 'reward', 'reward', 'ofc', 'ofc', 'ofc', 'ofc']
roi_names = ['E', 'F', 'M', 'S1', 'S2', 'S3', 'D1', 'D2', 'D3', 'D4', 'D5',
'D6', 'R1', 'R2', 'R3', 'R4', 'R5','O1', 'O2', 'O3', 'O4']
n_roi = len(roi_names)
M = np.random.rand(n_roi, n_roi) # array to plot
net_ind = sorted(np.unique(all_net_names, return_index=True)[1])
net_names = [all_net_names[index] for index in sorted(net_ind)]
network_pal = sns.husl_palette(len(net_names), s=.45)
network_lut = dict(zip(map(str, np.unique(all_net_names)), network_pal))
network_colors = pd.Series(all_net_names).map(network_lut)
network_colors = np.asarray(network_colors)
g = sns.clustermap(M, center=0, cmap="vlag",
row_cluster=False,
col_cluster=False,
row_colors=network_colors,
col_colors=network_colors,
linewidths=0, figsize=(10, 10))
g.ax_heatmap.set_xticklabels(roi_names, rotation=90)
g.ax_heatmap.set_yticklabels(roi_names, rotation=0)
它工作并提供以下输出:
我可以添加对应于每个单元格的标签,但我还想用唯一的网络名称标记每个集群,如下所示:
有什么办法可以做到这一点吗
也许在代码末尾添加这个
每个组的开头由
net_ind
给出。为了把标签放在很好的位置上,它们应该放在开始位置的中间位置和下一个标签的起始位置。由于最后一个组没有下一个标签,因此我们将all_net_names
的长度作为最后一个组的结尾可以对列执行相同的操作:
相关问题 更多 >
编程相关推荐