pandas 注释 seaborn 因子图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39147492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Annotate seaborn Factorplot
提问by gowithefloww
I would like to visualize 2 boolean informations stored as columns in one seaborn FactorPlot.
我想在一个 seaborn FactorPlot 中可视化存储为列的 2 个布尔信息。
Here is my df :
这是我的 df :
I would like to visualize both of actual_group
and adviced_group
in the same FactorPlot.
我想在同一个 FactorPlot 中可视化actual_group
和adviced_group
。
For now I am only able to plot the adviced_groups
using the hue
parameter :
现在我只能adviced_groups
使用hue
参数绘制:
with the code below :
使用以下代码:
_ = sns.factorplot(x='groups',
y='nb_opportunities',
hue='adviced_groups',
size=6,
kind='bar',
data=df)
I tried to use the ax.annotate()
from matplotlib without any success, because - for what I understood - Axes are not handled by the sns.FactorPlot()
method.
我尝试使用ax.annotate()
from matplotlib 没有任何成功,因为 - 据我所知 - 轴不是由该sns.FactorPlot()
方法处理的。
It could be an annotation, colorize one of the rectangle's edge or anything that could help visualize the actual group.
它可以是注释、为矩形的边缘着色或任何有助于可视化实际组的东西。
The result could be for instance something like this :
结果可能是这样的:
回答by Nickil Maveli
You could make use of plt.annotate
method provided by matplotlib
to make annotations for the factorplot
as shown:
您可以使用plt.annotate
提供的方法为如下所示matplotlib
进行注释factorplot
:
Setup:
设置:
df = pd.DataFrame({'groups':['A', 'B', 'C', 'D'],
'nb_opportunities':[674, 140, 114, 99],
'actual_group':[False, False, True, False],
'adviced_group':[False, True, True, True]})
print (df)
actual_group adviced_group groups nb_opportunities
0 False False A 674
1 False True B 140
2 True True C 114
3 False True D 99
Data Operations:
数据操作:
Choosing the subset of df
where the values of actual_group
are True. The index
value and the nb_opportunities
value become the arguments for x and y that become the location of the annotation.
选择df
值为actual_group
True的子集。该index
值和nb_opportunities
值成为X和Y是成为注释的位置参数。
actual_group = df.loc[df['actual_group']==True]
x = actual_group.index.tolist()[0]
y = actual_group['nb_opportunities'].values[0]
Plotting:
绘图:
sns.factorplot(x="groups", y="nb_opportunities", hue="adviced_group", kind='bar', data=df,
size=4, aspect=2)
Adding some padding to the location of the annotation as well as the location of text to account for the width of the bars being plotted.
添加一些填充到注释的位置以及文本的位置以考虑绘制的条的宽度。
plt.annotate('actual group', xy=(x+0.2,y), xytext=(x+0.3, 300),
arrowprops=dict(facecolor='black', shrink=0.05, headwidth=20, width=7))
plt.show()