Plotting improvements#513
Open
raphaelvallat wants to merge 6 commits into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #513 +/- ##
==========================================
+ Coverage 98.36% 98.70% +0.33%
==========================================
Files 19 19
Lines 3305 3313 +8
Branches 488 485 -3
==========================================
+ Hits 3251 3270 +19
+ Misses 32 28 -4
+ Partials 22 15 -7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR modernizes and extends Pingouin’s plotting utilities in src/pingouin/plotting.py, addressing compatibility issues with newer matplotlib versions, improving statistical correctness, and adding small API extensions for customization.
Changes:
- Fix plotting runtime issues and correctness bugs (e.g.,
plot_pairedboxplot alpha handling,qqplotnormalization condition). - Add new plotting options (
plot_blandaltman(percentage=...),qqplot(line_kwargs=..., ci_kwargs=...)) and improve Bland–Altman visuals. - Remove mutable default arguments across plotting functions and add targeted test coverage for new/changed branches.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
src/pingouin/plotting.py |
Implements plotting fixes, new parameters, improved annotations/styling, and safer default handling. |
tests/test_plotting.py |
Extends tests to cover new parameters and additional execution branches / error paths. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR improves all plotting functions in
pingouin/plotting.py. Changes span bug fixes, newparameters, API corrections, and code clarity improvements.
Bug fixes
plot_paired:ax.artistsreplaced withax.patchesThe boxplot transparency logic used
ax.artists, which was removed in recent versions ofmatplotlib. This caused an
AttributeErrorat runtime wheneverboxplot=True(the default). Itis now replaced with the correct
ax.patches.qqplot: normalization condition correctedThe condition that normalizes observed quantiles read:
This meant normalization was skipped whenever only one of
locorscalediffered from theidentity transform (for example, a fit with
loc=0butscale=2.5). The condition is now:Mutable default arguments fixed in
plot_paired,plot_rm_corr,plot_circmeanAll three functions used mutable objects (
listordict) as default parameter values. This is aknown Python pitfall: if a caller mutated the default object, all subsequent calls would be
affected. The defaults are now
Noneand the internal defaults are constructed fresh on each call.Affected parameters:
plot_pairedcolors,pointplot_kwargs,boxplot_kwargsplot_rm_corrkwargs_facetgrid,kwargs_line,kwargs_scatterplot_circmeankwargs_markers,kwargs_arrowThe default values are unchanged. Existing call sites that pass these arguments explicitly are
unaffected.
New functionality
plot_blandaltman:percentageparameterWhen
percentage=True, differences are expressed as(x - y) / mean(x, y) * 100. This is therecommended form of the Bland-Altman plot when measurement variability scales with magnitude or
when the two methods use different units/ranges. The y-axis label is updated to include
[%].qqplot:line_kwargsandci_kwargsparametersThe regression fit line and confidence envelope were previously hardcoded as red (
"r-"and"r--"). Two new optional parameters allow full control over their appearance:Both parameters accept any keyword arguments accepted by
matplotlib.pyplot.plot. Omitting themreproduces the previous default style.
Visual improvements to
plot_blandaltmanZero reference line
A light grey horizontal line is drawn at
y = 0(perfect agreement) behind all other plotelements. This is a standard component of Bland-Altman plots that was previously missing.
Symmetric y-axis
The y-axis is now forced to be symmetric around zero. This prevents visual bias when the mean
difference is close to zero but the axis happens to be asymmetric due to the data range.
Corrected annotation labels
The upper and lower limits of agreement (LoA) labels previously read
"+1.96 SD", which is onlyaccurate when
agreement=1.96. The labels now read+{agreement:.2f}×SDand−{agreement:.2f}×SDand are accurate for any value ofagreement.Decoupled CI band colors
The confidence interval bands for the limits of agreement previously inherited the scatter point
color. This meant that setting
color="tab:red"would also change the LoA CI bands to red. Thecolors are now independent:
tab:graytab:blueLimits of agreement line style
The LoA lines are now drawn with
linestyle="--"(dashed) instead oflinestyle=":"(dotted),which is the more common convention and improves legibility at small figure sizes.
Code clarity
plot_circmean: simplified default-mergingThe 10-line block of
if "key" not in dictchecks has been replaced with a single dict merge:_ppoints: added formula commentA short comment now links the
aconstant to Blom (1958), the original source for the two values(
3/8for small samples,0.5otherwise).plot_blandaltman: SE formula commentA comment on the LoA standard error formula now cites the original Bland and Altman (1986) paper,
making the non-obvious
sqrt(3 * s² / n)expression self-documenting.