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
27 changes: 27 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,30 @@ def test_filled_path_collection_date_xaxis():
filled = [t for t in plotly_fig.data if t.fill == "toself"]
assert len(filled) >= 1
assert all(isinstance(x, str) for x in filled[0].x)


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


def test_histogram_converts():
"""Histograms must convert without error and keep bargap in plotly's
valid [0, 1] range; get_bar_gap can return a gap with floating point
noise for touching bars, which plotly rejects."""
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