gpt4 book ai didi

python - 如何使用 seaborn pairgird 和 seaborn barplot 为每个图添加颜色和星号

转载 作者:行者123 更新时间:2023-11-28 18:26:03 32 4
gpt4 key购买 nike

我想绘制 3 个水平条形图,标签作为 y 轴,数据作为 x 轴,我希望每个图都是不同的颜色,并有某种类型的注释,例如星号,这取决于关于数据中某列所表示的重要性,例如:

dat = pd.DataFrame({
'Labels':['v1','v2','c1','c2'],
'Ave': [.2, .3, .5, .9],
'SD': [0.02, 0.1, 0.04, 0.06],
'Tot': [3, 4, 6, 8],
'Sig': [0.05, 0.001, 0.0001, 0.05]
})

sns.set_style('white')

g = sns.PairGrid(dat, x_vars=['Ave', 'SD', 'Tot'], y_vars=['Labels'])
g.map(sns.barplot)

会给我这样的东西:

enter image description here

如何让每个图“Ave”、“SD”和“ToT”成为它们自己的颜色?以及如何添加注释以表示“Sig”列给出的重要性?

最佳答案

您可以通过将数据 reshape 为长(整洁)格式并使用 factorplot 来接近。

reshape :

dat = (
pandas.DataFrame({
'Labels':['v1','v2','c1','c2'],
'Ave': [.2, .3, .5, .9],
'SD': [0.02, 0.1, 0.04, 0.06],
'Tot': [3, 4, 6, 8],
'Sig': [0.05, 0.001, 0.0001, 0.05]
}).set_index('Labels')
.unstack()
.reset_index()
.rename(columns={
'level_0': 'stat',
0: 'result'
})
)

print(dat.head(8))

stat Labels result
0 Ave v1 0.20
1 Ave v2 0.30
2 Ave c1 0.50
3 Ave c2 0.90
4 SD v1 0.02
5 SD v2 0.10
6 SD c1 0.04
7 SD c2 0.06

和因子图:

seaborn.factorplot('result', 'Labels', data=dat,
kind='bar', sharex=False,
hue='stat', hue_order=stats,
col='stat', col_order=stats)

enter image description here

问题是条形偏移了,因为使用 hue 告诉 seaborn 在每个 y 位置为不同的类别腾出空间。

在下一个版本中,您将能够使用 dodge=False 来避免这种偏移。

关于python - 如何使用 seaborn pairgird 和 seaborn barplot 为每个图添加颜色和星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41434815/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com