【发布时间】:2020-02-20 17:45:18
【问题描述】:
我有一个网络图。
每个节点都是一个案例,每个边都是一个 CPT。
我使用community.best_partition 将图表分成四个社区(用颜色表示)。
为了更好地可视化每个社区中的共享 CPT 和案例数量,我使用 plt.subplots 和 sns.heatmap 创建了四个社区之间具有相似匹配颜色的热图。
生成热图的代码:
fig, axs = plt.subplots(nrows=4, figsize=(16,8), sharex=True)
cmaps = ['Blues', 'Oranges', 'Greens', 'Reds']
comms = range(4)
for ax, cmap, comm in zip(axs, cmaps, comms):
sns.heatmap(
data=_.loc[[comm]],
ax=ax,
cmap=cmap,
annot=True,
annot_kws={
'fontsize' : 12
},
fmt='g',
cbar=False,
robust=True,
)
ax.set_ylabel('Community')
ax.set_xlabel('');
问题
sns.heatmap 中是否有一种方法可以按行(在本例中为社区)指定颜色,而无需创建 4 个单独的热图?
这是一些示例数据:
cpt 52320 52353 52310 49568 50432 52234 52317 50435 52354 52332
comm
0 NaN 3.0 NaN 1.0 1.0 NaN 2.0 2.0 NaN 3.0
1 1.0 30.0 NaN NaN NaN 1.0 NaN NaN NaN 20.0
2 NaN NaN 160.0 NaN NaN NaN NaN NaN NaN NaN
3 NaN 7.0 NaN NaN NaN NaN NaN NaN 1.0 12.0
【问题讨论】:
标签: python matplotlib seaborn heatmap