我想可视化2布尔信息存储为列在一个海运FactorPlot。
下面是我的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()方法处理的。
它可以是一个注释,给矩形的一条边着色,或者任何可以帮助可视化实际组的东西。
例如,结果可能是这样的:

发布于 2016-08-26 02:45:13
您可以使用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数据操作:
选择actual_group的值为True的df的子集。index值和nb_opportunities值成为x和y的参数,而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()

https://stackoverflow.com/questions/39147492
复制相似问题