在类似热图的图中写入值,但用于 seaborn 中的分类变量 [英] Write values in heatmap-like plot, but for categorical variables in seaborn

查看:60
本文介绍了在类似热图的图中写入值,但用于 seaborn 中的分类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类似热图的图中绘制了一个数据框,我想写入单元格,但不是单元格的值,但我将值与条件进行比较并说明它是哪种错误.

I plot a dataframe in a heatmap-like plot, and I would like to write to the cell, but not the value of the cell but I compare the value with conditions and tells which kind of error it is.

例如:

import pandas as pd 
import seaborn as sns # matplotlib inline 
import random
data = []
for i in range(10):
    data.append([random.randrange(0, 11, 1) for _ in range(10)])
df = pd.DataFrame(data)
n = 10

fig, ax = plt.subplots(figsize = (12, 10)) 
cmap = ['#b3e6b3','#66cc66','#2d862d','#ffc299','#ff944d','#ff6600','#ccddff','#99bbff','#4d88ff','#0044cc','#002b80']
ax = sns.heatmap(df, cmap=cmap, linewidths = 0.005, annot = False) 
                            
plt.show()

运行这段代码时我得到的是:

What I got when running this code is:

然后我将数据帧 df 与某些条件进行比较,并获得另外 2 个数据帧,例如:

Then I compare dataframe df with some conditions and get 2 other dataframes, for example:

condition1 = [['Error A'] + [np.nan]*9,
            [np.nan]*6 + ['Error C'] + [np.nan]*3,
            [np.nan]*10,
            [np.nan]*7 + ['Error B'] + [np.nan]*2,
            [np.nan]*2 + ['Error D'] + [np.nan]*3 + ['Error B'] + [np.nan]*3,
            [np.nan]*10,
            [np.nan]*3 + ['Error B'] + [np.nan]*6,
            [np.nan]*7 + ['Error A'] + [np.nan]*2,
            [np.nan]*10,
            [np.nan]*10]
df_condition1 = pd.DataFrame(data = condition1)

condition2 = [[np.nan]*10,[np.nan]*10,
            [np.nan]*10,[np.nan]*7 + ['Error C'] + [np.nan]*2,
            [np.nan]*10,[np.nan]*10,[np.nan]*10,
            [np.nan]*10,
            [np.nan]*10,
            [np.nan]*10]
df_condition2 = pd.DataFrame(data = condition2)

我想要的是在热图中显示这些数据帧的值,如下所示:

and what I want is to show values of these dataframes in the heatmap, like this:

我该怎么做?

推荐答案

您可以手动构建错误文本并进行注释:

You can build the error texts and annotate manually:

c1, c2 = df_condition1.notna(), df_condition2.notna()
df_condition1,df_condition2 = df_condition1.fillna(''), df_condition2.fillna('')

errors = np.select((c1&c2, c1, c2), 
                   (df_condition1+'\n'+df_condition2, df_condition1, df_condition2),
                   '')

fig, ax = plt.subplots(figsize = (12, 10)) 
cmap = ['#b3e6b3','#66cc66','#2d862d','#ffc299','#ff944d','#ff6600','#ccddff','#99bbff','#4d88ff','#0044cc','#002b80']
ax = sns.heatmap(df, cmap=cmap, linewidths = 0.005, annot = False) 
    
for r in range(errors.shape[0]):
    for c in range(errors.shape[1]):
        ax.text(c+0.5,r+0.5, errors[r,c], 
                va='center',ha='center',
                fontweight='bold')


plt.show()

输出:

这篇关于在类似热图的图中写入值,但用于 seaborn 中的分类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆