fix: Fix flaky matplotlib-to-plotly conversion of touching bars (negative bargap) - #5696
Open
robertoffmoura wants to merge 1 commit into
Open
robertoffmoura wants to merge 1 commit into
robertoffmoura wants to merge 1 commit into
Conversation
robertoffmoura
force-pushed
the
rm/fix-negative-bargap
branch
from
August 21, 2026 18:09
7f314bd to
8437938
Compare
robertoffmoura
force-pushed
the
rm/fix-negative-bargap
branch
from
August 31, 2026 17:37
8437938 to
286fce0
Compare
robertoffmoura
force-pushed
the
rm/fix-negative-bargap
branch
from
September 3, 2026 11:40
286fce0 to
ba3fc37
Compare
camdecoster
requested changes
Sep 24, 2026
camdecoster
left a comment
Contributor
There was a problem hiding this comment.
Could you please make a couple of changes and add a CHANGELOG entry?
Comment on lines
+272
to
+274
| # plotly's bargap must be in [0, 1]; clamp to guard against | ||
| # floating point noise (e.g. -8.9e-16 for touching bars) | ||
| return min(max(gap0, 0.0), 1.0) |
Contributor
There was a problem hiding this comment.
We need to normalize gap0 to calculate the correct bargap.
Suggested change
| # plotly's bargap must be in [0, 1]; clamp to guard against | |
| # floating point noise (e.g. -8.9e-16 for touching bars) | |
| return min(max(gap0, 0.0), 1.0) | |
| # Plotly's `bargap` is a fraction of the distance between bar positions, not a gap in data units | |
| # so we need to normalize `gap0` with `bar_delta` | |
| bar_delta = bar_starts[1] - bar_starts[0] | |
| if bar_delta <= 0: | |
| return None | |
| # Clamp to guard against floating point noise, such as -8.9e-16 for touching bars | |
| return min(max(gap0 / bar_delta, 0.0), 1.0) |
Comment on lines
+242
to
+248
| fig, ax = plt.subplots() | ||
| ax.hist(np.random.randn(1000), 30) | ||
|
|
||
| plotly_fig = tls.mpl_to_plotly(fig) | ||
|
|
||
| assert len(plotly_fig.data) == 1 | ||
| assert 0 <= plotly_fig.layout.bargap <= 1 |
Contributor
There was a problem hiding this comment.
Let's make this test deterministic:
Suggested change
| fig, ax = plt.subplots() | |
| ax.hist(np.random.randn(1000), 30) | |
| plotly_fig = tls.mpl_to_plotly(fig) | |
| assert len(plotly_fig.data) == 1 | |
| assert 0 <= plotly_fig.layout.bargap <= 1 | |
| # Seed 0 makes the first gap slightly negative (-4.4e-16) | |
| rng = np.random.RandomState(0) | |
| fig, ax = plt.subplots() | |
| ax.hist(rng.randn(10000), 30) | |
| plotly_fig = tls.mpl_to_plotly(fig) | |
| assert len(plotly_fig.data) == 1 | |
| assert plotly_fig.layout.bargap == 0 |
This branch has not been deployed
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.
mpl_to_plotly intermittently crashes when converting figures with touching bars (e.g. plt.hist). The bar gap is computed as bar_start[i+1] - bar_end[i], and for adjacent bars floating-point noise can make this a tiny negative number (e.g. -8.88e-16). Plotly's bargap property only accepts values in [0, 1], so the conversion fails with:
Reproduces with:
Fix: get_bar_gap now clamps the gap to [0, 1], so float noise around zero maps to bargap = 0 (touching bars) and genuine gaps are unchanged.