Write Values In Heatmap-like Plot, But For Categorical Variables In 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 erro
Solution 1:
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()
Output:
Post a Comment for "Write Values In Heatmap-like Plot, But For Categorical Variables In Seaborn"