gpt4 book ai didi

python - 在类似热图的图中写入值,但对于 seaborn 中的分类变量

转载 作者:行者123 更新时间:2023-12-04 03:51:35 24 4
gpt4 key购买 nike

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

例如:

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()

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

this is without values

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

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)

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

what I want

我该怎么做?

最佳答案

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

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()

输出:

enter image description here

关于python - 在类似热图的图中写入值,但对于 seaborn 中的分类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64373550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com