Skip to content

Commit 1130276

Browse files
Clamp bargap to [0, 1] in get_bar_gap
1 parent a5974ce commit 1130276

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
@@ -199,3 +199,17 @@ def test_filled_path_collection_date_xaxis():
199199
filled = [t for t in plotly_fig.data if t.fill == "toself"]
200200
assert len(filled) >= 1
201201
assert all(isinstance(x, str) for x in filled[0].x)
202+
203+
204+
def test_get_bar_gap_clamps_negative_float_noise():
205+
"""Touching bars can produce a tiny negative gap from floating point
206+
noise (e.g. -8.88e-16 for a histogram); plotly rejects bargap outside
207+
[0, 1], so the gap must be clamped."""
208+
from plotly.matplotlylib.mpltools import get_bar_gap
209+
210+
# touching bars: gap is exactly 0
211+
assert get_bar_gap([0.0, 1.0], [1.0, 2.0]) == 0.0
212+
# overlapping-by-noise bars: gap is a tiny negative float, clamped to 0
213+
assert get_bar_gap([0.0, 1.0], [1.0 + 1e-15, 2.0]) == 0.0
214+
# positive gaps are unchanged
215+
assert get_bar_gap([0.0, 2.0], [1.0, 3.0]) == 1.0

0 commit comments

Comments
 (0)