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.
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.
- ✅ 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
# Clone the repository
git clone https://github.com/cjohnsoncruz/plot_posthoc_test.git
cd plot_posthoc_test
# Install in editable mode
pip install -e .python >= 3.8
numpy
pandas
scipy
matplotlib
seaborn < 0.13
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()andplot_sig_bars_w_comp_df_tight()are still available for backward compatibility.
- 📚 Package Structure - Detailed package organization
- 📖 Usage Examples - More code examples and use cases
- 📝 Changelog - Version history and updates
- 🔗 API Reference - Complete API documentation
- 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
Currently available via GitHub. PyPI upload scheduled for Q3 2025.
Contributions are welcome! Please feel free to submit a Pull Request.
- 💬 GitHub Discussions - Ask questions and share ideas
- 🐛 GitHub Issues - Report bugs and request features
MIT License - see LICENSE file for details
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
Carlos Johnson-Cruz (@cjohnsoncruz)
Version: 0.1.0 | Updated: October 2025