pythonmatplotlibstatisticsseaborn

Statistical significance on matplotlib / seaborn graph?


I finished analyzing my data and want to show that they are statistically significant using the t-test_ind. However, I haven't found anything functional to show this other than what was referenced in (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')

Using this approach however, whenever I try to save the plot using plt.savefig() the added significancies using the add_stat_annotation are discared (matplotlib does not seem to recognize the added annotations). Using the loc='inside' option messes up my plot so it isn't really an option.

I am therefore asking if there is some simpler way to add the sigificancies directly in matplotlib / seaborn or if you can plt.savefig() with enough border / padding to include everything.


Solution

  • It was mainly a xlabel cut off problem. So in future applications I would use the add_stat_annotation from webermarcolivier/statannot. To save your files use one of the following possibilities:

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

    Hope this will help someone for future use.