离散热图,根据变量更改单元格不透明度(seaborn?)

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

我想制作一个包含连续变量和样本附带的分类数据的热图。目标是获得来自类别的色调和来自连续值的不透明度(或透明度、饱和度)。

玩具数据集就像:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

names = ['Berthe', 'Paul', 'Francis', 'Jane']

time_spent_together = [
    [ 1.0, 0.2, 0.7, 0.5 ],
    [ 0.2, 1.0, 0.8, 0.5 ],
    [ 0.7, 0.8, 1.0, 0.1 ],
    [ 0.5, 0.5, 0.1, 1.0 ],
]

type_of_relationship = [
    [ 'id', 'friends', 'coworkers', 'nemesis' ],
    [ 'friends', 'id', 'family', 'family' ],
    [ 'coworkers', 'family', 'id', 'friends' ],
    [ 'nemesis', 'family', 'friends', 'id' ],
]

df_times = pd.DataFrame(data=time_spent_together, index=names, columns=names)
df_relationships = pd.DataFrame(data=type_of_relationship, index=names, columns=names)

结果会“改变”离散图:

plt.figure(figsize=(3,3))
value_to_int = {j:i for i,j in enumerate(pd.unique(df_relationships.values.ravel()))}
n = len(value_to_int)
cmap = sns.color_palette("tab10", n) 
sns.heatmap(df_relationships.replace(value_to_int), cmap=cmap)

随着连续的

plt.figure(figsize=(3,3))
sns.heatmap(df_times, cmap='Greys',annot=True, 
            annot_kws={"size": 7}, vmin=0.25, vmax=1)

如您所见,我使用了

seaborn
pyplot
。我很难超越基本行为。能够直接设置单元格的颜色可能是正确的路径?

预先感谢您的回答, 干杯!

python matplotlib seaborn visualization heatmap
1个回答
0
投票

您可以将两张图放在一起绘制,一张带有颜色,一张带有从透明到白色的 cmap:

import matplotlib as mpl
import matplotlib.colors as mcolors

plt.figure(figsize=(4, 3))

# categories
value_to_int = {j:i for i,j in enumerate(pd.unique(df_relationships.values.ravel()))}
n = len(value_to_int)
cmap = sns.color_palette("tab10", n) 
ax = sns.heatmap(df_relationships.replace(value_to_int), cmap=cmap)

# values
c_alpha = mcolors.colorConverter.to_rgba('white', alpha=0)
c_white = mcolors.colorConverter.to_rgba('white', alpha=1)
alpha_cmap = mcolors.LinearSegmentedColormap.from_list('alpha_cmap', [c_alpha,c_white], 512)
ax2 = sns.heatmap(df_times.rsub(1), cmap=alpha_cmap, annot=True, 
            annot_kws={"size": 7}, vmin=0.25, vmax=1, ax=ax, cbar=None)
ax.figure.colorbar(mpl.cm.ScalarMappable(mcolors.Normalize(vmin=0.25, vmax=1), cmap='Greys_r'), ax=ax2)

输出:

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