Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在sns clustermap中标记群集_Python_Cluster Computing_Seaborn - Fatal编程技术网

Python 如何在sns clustermap中标记群集

Python 如何在sns clustermap中标记群集,python,cluster-computing,seaborn,Python,Cluster Computing,Seaborn,我正在使用以下代码创建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',

我正在使用以下代码创建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)
它工作并提供以下输出:

我可以添加对应于每个单元格的标签,但我还想用唯一的网络名称标记每个集群,如下所示:


有什么想法可以实现吗?

也许可以在代码末尾添加这个

g.ax\u row\u colors.set\u yticks(0.5*(np.array(net\u ind)+np.array(net\u ind[1:][len(all\u net\u names)])
g、 ax\u行\u颜色。设置\u标签(网络名称)
g、 ax_row_colors.yaxis.set_tick_params(size=0)#使勾号不可见
每组的开头由
net\u ind
给出。为了把标签放在很好的位置上,它们应该放在开始位置的中间位置和下一个标签的起始位置。由于最后一组没有下一个标签,我们将
所有网络名称的长度
作为最后一组的结束

可以对列执行相同的操作:

g.ax\u col\u colors.set\u xticks(0.5*(np.array(net\u ind)+np.array(net\u ind[1:][len(all\u net\u names)])
g、 ax\u col\u colors.set\u xticklabels(净名称,旋转=90)
g、 ax_col_colors.xaxis.set_tick_params(size=0)#使勾号不可见
g、 ax\u col\u colors.xaxis.tick\u top()

这是代码中的一个错误。这是我在“所有网络名称”中使用的旧名称。谢谢你指出。