Skip to content

cjohnsoncruz/plot_posthoc_test

Repository files navigation

plot_posthoc_test

Python Version License: MIT

A Python package for performing and plotting post-hoc statistical tests on matplotlib/seaborn visualizations. Inspired by statannotations, but with more direct control over plotting parameters and a streamlined, user-friendly API.

Description

Post-hoc tests are essential for statistical analysis involving multiple groups (learn more). This package provides functions for performing and annotating post-hoc tests on pandas DataFrames with matplotlib and seaborn plots.

Note: Currently optimized for seaborn pointplots. Requires seaborn < 0.13 to access hue groups in collections.

Features

  • Statistical Tests: Mann-Whitney U, Cohen's d, permutation tests, t-tests
  • Smart Annotations: Automatically places significance bars between groups
  • Hue Support: Handles complex hue groupings in seaborn plots
  • Error Bar Detection: Adjusts bar placement based on error bars
  • User-Friendly Aliases: Simple API for common operations
  • Modular Design: Organized utility functions for stats, plotting, and axis manipulation

Installation

From GitHub (Development Mode)

# Clone the repository
git clone https://github.com/cjohnsoncruz/plot_posthoc_test.git
cd plot_posthoc_test

# Install in editable mode
pip install -e .

Requirements

python >= 3.8
numpy
pandas
scipy
matplotlib
seaborn < 0.13

Quick Start

import plot_posthoc_test as ppt
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

# Create your plot
fig, ax = plt.subplots()
sns.pointplot(data=df, x='condition', y='value', hue='group', ax=ax)

# Define which groups to compare
comparisons = [('control', 'treatment'), ('control', 'other')]

# Run statistical tests and annotate
plot_params = {'data': df, 'x': 'condition', 'y': 'value', 'hue': 'group'}
posthoc_df = ppt.annotate(ax, plot_params, ax, comparisons, test_name='MWU')

# Plot significance bars for p < 0.05
ppt.plot_significance(ax, posthoc_df[posthoc_df['pvalue'] < 0.05])

# Optional: Convert p-values to asterisks
stars = ppt.stars(0.001)  # Returns "***"

plt.show()

Note: Original function names like main_run_posthoc_tests_and_get_hue_loc_df() and plot_sig_bars_w_comp_df_tight() are still available for backward compatibility.

Documentation

Available Statistical Tests

  • Mann-Whitney U (MWU): Non-parametric test for independent samples
  • Cohen's d: Effect size measure with confidence intervals
  • Permutation test: Non-parametric resampling test
  • Two-sample t-test: Parametric test (Welch's for unequal variance)
  • Bootstrap standard deviation overlap: Custom distribution separation test

Availability

Currently available via GitHub. PyPI upload scheduled for Q3 2025.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Communication

License

MIT License - see LICENSE file for details

Acknowledgements

This package was inspired by and includes concepts derived from statannotations by Florian Charlier (@trevismd).

plot_posthoc_test builds upon these ideas with:

  • More direct control over plotting parameters
  • Streamlined function-oriented API
  • Enhanced hue group handling for complex seaborn plots
  • Modular utility structure for statistical tests and axis manipulation

Author

Carlos Johnson-Cruz (@cjohnsoncruz)


Version: 0.1.0 | Updated: October 2025

About

Repository containing custom code inspired by statannot's codebase, but with greatly streamlined functionality

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages