注释 seaborn Factorplot
Annotate seaborn Factorplot
我想可视化存储为一个 seaborn FactorPlot 中的列的 2 个布尔信息。
这是我的 df :
我想在同一个 FactorPlot 中可视化 actual_group
和 adviced_group
。
现在我只能使用 hue
参数绘制 adviced_groups
:
使用以下代码:
_ = sns.factorplot(x='groups',
y='nb_opportunities',
hue='adviced_groups',
size=6,
kind='bar',
data=df)
我尝试使用 matplotlib 中的 ax.annotate()
但没有成功,因为 - 据我了解 - sns.FactorPlot()
方法不处理轴。
它可以是注释、为矩形的边缘之一着色或任何有助于可视化实际组的内容。
结果可能是这样的:
你可以利用matplotlib
提供的plt.annotate
方法为factorplot
做注解,如图:
设置:
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
数据操作:
选择 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]
绘图:
sns.factorplot(x="groups", y="nb_opportunities", hue="adviced_group", kind='bar', data=df,
size=4, aspect=2)
在注释的位置以及文本的位置添加一些填充,以说明绘制的条形的宽度。
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()
我想可视化存储为一个 seaborn FactorPlot 中的列的 2 个布尔信息。
这是我的 df :
我想在同一个 FactorPlot 中可视化 actual_group
和 adviced_group
。
现在我只能使用 hue
参数绘制 adviced_groups
:
使用以下代码:
_ = sns.factorplot(x='groups',
y='nb_opportunities',
hue='adviced_groups',
size=6,
kind='bar',
data=df)
我尝试使用 matplotlib 中的 ax.annotate()
但没有成功,因为 - 据我了解 - sns.FactorPlot()
方法不处理轴。
它可以是注释、为矩形的边缘之一着色或任何有助于可视化实际组的内容。
结果可能是这样的:
你可以利用matplotlib
提供的plt.annotate
方法为factorplot
做注解,如图:
设置:
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
数据操作:
选择 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]
绘图:
sns.factorplot(x="groups", y="nb_opportunities", hue="adviced_group", kind='bar', data=df,
size=4, aspect=2)
在注释的位置以及文本的位置添加一些填充,以说明绘制的条形的宽度。
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()