使用matplotlib / seaborn在barplot上的统计意义?

时间:2019-05-29 14:13:00

标签: python matplotlib statistics seaborn

我完成了对数据的分析,并希望使用t-test_ind来显示它们在统计上是有意义的。但是,除了(How does one insert statistical annotations (stars or p-values) into matplotlib / seaborn plots?)中引用的内容外,我没有发现其他功能可以显示此内容:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
from statannot import add_stat_annotation

ax = sns.barplot(x=x, y=y, order=order)
add_stat_annotation(ax, data=df, x=x, y=y,
                    boxPairList=[(order[0], order[1]), (order[0], order[2])],
                    test='t-test_ind',
                    textFormat='star',
                    loc='outside')

但是,使用这种方法时,每当我尝试使用plt.savefig()保存地块时,都不会使用add_stat_annotation来增加重要性(matplotlib似乎无法识别所添加的注释)。使用loc='inside'选项会弄乱我的情节,因此实际上不是一个选择。

因此,我问是否有一些更简单的方法可以直接在matplotlib / seaborn中添加标记,或者是否可以plt.savefig()使用足够的边框/填充来包含所有内容。

1 个答案:

答案 0 :(得分:0)

主要是xlabel中断问题。因此,在将来的应用程序中,我将像以前一样使用add_stat_annotation(确保使用分类数据),并使用以下一种可能性:

import matplotlib.pyplot as plt

plt.tight_layout() # Option 1
plt.autoscale()    # Option 2
plt.savefig('filename.png', bbox_inches = "tight") # Option 3

希望这将对将来的使用有所帮助。

相关问题