如何在Seaborn Heatmap单元中显示多个注释? [英] How to display multiple annotations in Seaborn Heatmap cells?

查看:62
本文介绍了如何在Seaborn Heatmap单元中显示多个注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Seaborn热图在热图的每个单元格中显示多个值.这是我想看到的手动示例,请清楚一点:

I want seaborn heatmap to display multiple values in each cell of the heatmap. Here is a manual example of what I want to see, just to be clear:

data = np.array([[0.000000,0.000000],[-0.231049,0.000000],[-0.231049,0.000000]])
labels =  np.array([['A\nExtra Stuff','B'],['C','D'],['E','F']])
fig, ax = plt.subplots()
ax = sns.heatmap(data, annot = labels, fmt = '')

以下是获取seaborn.heat以在单元格中显示 flightsRou​​ndUp 值的示例.

Here as an example to get seaborn.heat to display flightsRoundUp values in the cells.

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

def RoundUp(x):
    return int(np.ceil(x/10)*10)

# Load the example flights dataset and conver to long-form
flights_long = sns.load_dataset("flights")
flights = flights_long.pivot("month", "year", "passengers")
flightsRoundUp =  flights.applymap(RoundUp)

# Draw a heatmap with the numeric values in each cell
f, ax = plt.subplots(figsize=(9, 6))
sns.heatmap(flights, annot=flightsRoundUp, fmt="", linewidths=.5, ax=ax)

在所有单元格中同时显示 flightsRou​​ndUp flights 的最佳方法是什么?类似于上面的第一个手动示例,但是对于所有单元格来说都是矢量化方式...

What is the best way to display both flightsRoundUp and flights in all cells? Something like the first manual example above, but for all the cells in a vectorized-like way...

推荐答案

Rotail的答案对我不起作用,应用该lambda函数时出现错误.

Rotail's answer didn't work for me, I got an error when applying that lambda function.

但是,我找到了一种解决方案,该解决方案利用了seaborn在彼此之上绘制连续图形的事实.您要做的就是使用一个调用热图来建立图形,然后再调用每个注释.使用annot_kws arg来确保文本不会互相覆盖.

However, I found a solution that exploits the fact that seaborn plots sequential figures on top of each other. All you have to do is use one call to heatmap to establish the figure, and then a subsequent call for each of the annotations. Use the annot_kws arg to make sure the text aren't written over eachother.

X = pd.DataFrame({'a':[1, 2, 3], 'b':[4, 5, 6]})
Y = pd.DataFrame({'A':['A', 'B', 'C'], 'B':['E', 'F', 'G']})
Z = pd.DataFrame({'A':['(Extra Stuff)', '(Extra Stuff)', '(Extra Stuff)'], 'B':['(Extra Stuff)', '(Extra Stuff)', '(Extra Stuff)']})

sns.heatmap(X, annot=False)
sns.heatmap(X, annot=Y, annot_kws={'va':'bottom'}, fmt="", cbar=False)
sns.heatmap(X, annot=Z, annot_kws={'va':'top'}, fmt="", cbar=False)

这篇关于如何在Seaborn Heatmap单元中显示多个注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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