如何使用文本标签在Seaborn Heatmap中自定义颜色栏

问题描述 投票:0回答:1

我想为热图创建一个颜色条图例,它不是数字而是文本。

例如,假设我要显示在某些城市之间穿越的人口分布。但是在颜色栏中,我要有三个文本“低人口”,“中”和“高”,而不是显示人口范围的数字。

根本不是专业人士,但是我要解决此问题的代码受到打击:

ax = sns.heatmap(Matrix_of_Population, cmap='viridis', cbar_kws={'label': 'Low      Medium    high'})

但是人口数量也不是这些文本。

python matplotlib legend heatmap colorbar
1个回答
0
投票

您可以在颜色栏中为特定刻度指定标签:

import seaborn as sns
import numpy as np 


a = np.random.randn(20,20)
ax = sns.heatmap(a, cmap='viridis')
c_bar = ax.collections[0].colorbar
c_bar.set_ticks([-2, 0, 2])
c_bar.set_ticklabels(['Low', 'Medium', 'High'])

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.