Skip to content

Commit 1fcac15

Browse files
Clamp bargap to [0, 1] in get_bar_gap
1 parent c0740bf commit 1fcac15

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

plotly/matplotlylib/mpltools.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ def get_bar_gap(bar_starts, bar_ends, tol=1e-10):
269269
gap0 = gaps[0]
270270
uniform = all([abs(gap0 - gap) < tol for gap in gaps])
271271
if uniform:
272-
return gap0
272+
# plotly's bargap must be in [0, 1]; clamp to guard against
273+
# floating point noise (e.g. -8.9e-16 for touching bars)
274+
return min(max(gap0, 0.0), 1.0)
273275

274276

275277
def convert_rgba_array(color_list):

plotly/matplotlylib/tests/test_renderer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ def test_multiple_traces_native_legend():
8484
assert plotly_fig.data[0].mode == "lines"
8585
assert plotly_fig.data[1].mode == "markers"
8686
assert plotly_fig.data[2].mode == "lines+markers"
87+
88+
89+
def test_get_bar_gap_clamps_negative_float_noise():
90+
"""Touching bars can produce a tiny negative gap from floating point
91+
noise (e.g. -8.88e-16 for a histogram); plotly rejects bargap outside
92+
[0, 1], so the gap must be clamped."""
93+
from plotly.matplotlylib.mpltools import get_bar_gap
94+
95+
# touching bars: gap is exactly 0
96+
assert get_bar_gap([0.0, 1.0], [1.0, 2.0]) == 0.0
97+
# overlapping-by-noise bars: gap is a tiny negative float, clamped to 0
98+
assert get_bar_gap([0.0, 1.0], [1.0 + 1e-15, 2.0]) == 0.0
99+
# positive gaps are unchanged
100+
assert get_bar_gap([0.0, 2.0], [1.0, 3.0]) == 1.0

0 commit comments

Comments
 (0)