Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion plotly/matplotlylib/mpltools.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ def get_bar_gap(bar_starts, bar_ends, tol=1e-10):
gap0 = gaps[0]
uniform = all([abs(gap0 - gap) < tol for gap in gaps])
if uniform:
return gap0
# 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)


def convert_rgba_array(color_list):
Expand Down
14 changes: 14 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,17 @@ def test_multiple_traces_native_legend():
assert plotly_fig.data[0].mode == "lines"
assert plotly_fig.data[1].mode == "markers"
assert plotly_fig.data[2].mode == "lines+markers"


def test_get_bar_gap_clamps_negative_float_noise():
"""Touching bars can produce a tiny negative gap from floating point
noise (e.g. -8.88e-16 for a histogram); plotly rejects bargap outside
[0, 1], so the gap must be clamped."""
from plotly.matplotlylib.mpltools import get_bar_gap

# touching bars: gap is exactly 0
assert get_bar_gap([0.0, 1.0], [1.0, 2.0]) == 0.0
# overlapping-by-noise bars: gap is a tiny negative float, clamped to 0
assert get_bar_gap([0.0, 1.0], [1.0 + 1e-15, 2.0]) == 0.0
# positive gaps are unchanged
assert get_bar_gap([0.0, 2.0], [1.0, 3.0]) == 1.0