基于Python的seaborn中的比率的热图 [英] heatmap based on ratios in Python's seaborn
问题描述
我有笛卡尔坐标系中的数据。对于每个笛卡尔坐标,还有二进制变量。我想制作一个热图,其中在每个多边形(六边形/矩形等)中,颜色强度是该多边形中总出现次数中布尔值为True的出现次数的比率。
I have data in Cartesian coordinates. To each Cartesian coordinate there is also binary variable. I wan to make a heatmap, where in each polygon (hexagon/rectangle,etc.) the color strength is the ratio of number of occurrences where the boolean is True out of the total occurrences in that polygon.
例如,数据看起来像这样:
The data can for example look like this:
df = pd.DataFrame([[1,2,False],[-1,5,True], [51,52,False]])
我知道 seaborn
可以通过 seaborn.heatmap ,但是默认情况下,颜色强度基于每个多边形中的总出现次数,而不是上述比率。
I know that seaborn
can generate heatmaps via seaborn.heatmap, but the color strength is based by default on the total occurrences in each polygon, not the above ratio. Is there perhaps another plotting tool that would be more suitable?
推荐答案
您也可以使用熊猫 groupby
功能来计算比率,然后将结果传递给seaborn.heatmap。从@ImportanceOfBeingErnest借来的示例数据看起来像这样:
You could also use the pandas groupby
functionality to compute the ratios and then pass the result to seaborn.heatmap. With the example data borrowed from @ImportanceOfBeingErnest it would look like this:
import numpy as np
import pandas as pd
import seaborn as sns
np.random.seed(0)
x = np.random.poisson(5, size=200)
y = np.random.poisson(7, size=200)
z = np.random.choice([True, False], size=200, p=[0.3, 0.7])
df = pd.DataFrame({"x" : x, "y" : y, "z":z})
res = df.groupby(['y','x'])['z'].mean().unstack()
ax = sns.heatmap(res)
ax.axis('equal')
ax.invert_yaxis()
如果您的 x
和 y
值不是整数,您可以将其切成所需的类别进行分组:
If your x
and y
values aren't integers you can cut them into the desired number of categories for grouping:
bins = 10
res = df.groupby([pd.cut(df.y, bins),pd.cut(df.x,bins)])['z'].mean().unstack()
这篇关于基于Python的seaborn中的比率的热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!