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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:53:44  来源:igfitidea点击:

Annotate seaborn Factorplot

pythonpandasmatplotlibseaborn

提问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 :

enter image description here

在此处输入图片说明

I would like to visualize both of actual_groupand adviced_groupin the same FactorPlot.

我想在同一个 FactorPlot 中可视化actual_groupadviced_group

For now I am only able to plot the adviced_groupsusing the hueparameter :

现在我只能adviced_groups使用hue参数绘制:

enter image description here

在此处输入图片说明

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 :

结果可能是这样的:

enter image description here

在此处输入图片说明

回答by Nickil Maveli

You could make use of plt.annotatemethod provided by matplotlibto make annotations for the factorplotas 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 dfwhere the values of actual_groupare True. The indexvalue and the nb_opportunitiesvalue become the arguments for x and y that become the location of the annotation.

选择df值为actual_groupTrue的子集。该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()

Image

图片