trevismd / statannotations

add statistical significance annotations on seaborn plots. Further development of statannot, with bugfixes, new features, and a different API.
Other
639 stars 71 forks source link

plot and annotate, use in FacetGrid #29

Closed trevismd closed 3 years ago

trevismd commented 3 years ago

As requested here: https://github.com/webermarcolivier/statannot/issues/27, https://github.com/webermarcolivier/statannot/issues/82,

This PR provides the possibility to instantiate an Annotator without ax (from previously created plot), allowing its use in a FacetGrid

So, with this code (upcoming part 2 of tutorial)

sf = rfs.loc[rfs.State.isin(["Successful", "Failed"]), :]

plotting_parameters = {
    'x':       'Subcategory',
    'y':       'Goal',
    'order':   subcat_order,
    'palette': subcat_palette,
}

pairs = [('Robots', 'Flight'),
         ('Flight', 'Sound'),
         ('Robots', 'Sound')]

with sns.plotting_context("notebook", font_scale=1.4):
    annot = Annotator(None, pairs)

    g = sns.FacetGrid(sf, col='State', height=12, sharey=False)

    g.map_dataframe(annot.plot_and_annotate_facets, plot='boxplot',
                    plot_params=plotting_parameters,
                    configuration={"test": "Mann-Whitney"}, annotation_func="apply_test",
                    ax_op_before=[["set_yscale", ["log"], {}]])

    g.set_axis_labels("Technology Subcategory", "Goal ($)")
    g.fig.patch.set_alpha(1)

    plt.show()

We can get

p-value annotation legend:
      ns: p <= 1.00e+00
       *: 1.00e-02 < p <= 5.00e-02
      **: 1.00e-03 < p <= 1.00e-02
     ***: 1.00e-04 < p <= 1.00e-03
    ****: p <= 1.00e-04

Robots vs. Flight: Mann-Whitney-Wilcoxon test two-sided, P_val:9.847e-02 U_stat=7.500e+03
Flight vs. Sound: Mann-Whitney-Wilcoxon test two-sided, P_val:7.225e-01 U_stat=1.013e+04
Robots vs. Sound: Mann-Whitney-Wilcoxon test two-sided, P_val:1.671e-04 U_stat=2.491e+04
p-value annotation legend:
      ns: p <= 1.00e+00
       *: 1.00e-02 < p <= 5.00e-02
      **: 1.00e-03 < p <= 1.00e-02
     ***: 1.00e-04 < p <= 1.00e-03
    ****: p <= 1.00e-04

Robots vs. Flight: Mann-Whitney-Wilcoxon test two-sided, P_val:9.220e-02 U_stat=3.441e+04
Flight vs. Sound: Mann-Whitney-Wilcoxon test two-sided, P_val:8.043e-01 U_stat=3.803e+04
Robots vs. Sound: Mann-Whitney-Wilcoxon test two-sided, P_val:4.189e-02 U_stat=3.364e+04

FacetGrid result

codecov[bot] commented 3 years ago

Codecov Report

Merging #29 (1f21f03) into master (d491a76) will increase coverage by 0.25%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #29      +/-   ##
==========================================
+ Coverage   96.70%   96.96%   +0.25%     
==========================================
  Files          28       29       +1     
  Lines        1732     1845     +113     
==========================================
+ Hits         1675     1789     +114     
+ Misses         57       56       -1     
Impacted Files Coverage Δ
statannotations/Annotator.py 91.60% <100.00%> (+1.60%) :arrow_up:
statannotations/PValueFormat.py 96.62% <100.00%> (ø)
statannotations/_Plotter.py 97.38% <100.00%> (ø)
statannotations/_version.py 100.00% <100.00%> (ø)
statannotations/utils.py 100.00% <100.00%> (ø)
tests/test_annotator.py 100.00% <100.00%> (ø)
tests/test_integrate_annotator.py 100.00% <100.00%> (ø)
tests/test_pvalue_format.py 100.00% <100.00%> (ø)
tests/test_utils.py 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update d491a76...1f21f03. Read the comment docs.

DDaffieS commented 6 months ago

Hi, I was wondering what the code would look like for custom annotations, e.g., annotation_func='set_pvalues' or annotation_func= 'set_custom_annotations'? What are the parameters that need to be specified in the annotation_params dictionary? I can't find this specified in the statanotations documentation (https://statannotations.readthedocs.io/en/latest/statannotations.html#statannotations.Annotator.Annotator.plot_and_annotate_facets) Thanks!