Skip to content

Commit ba3fc37

Browse files
Clamp bargap to [0, 1] in get_bar_gap
1 parent d3105d4 commit ba3fc37

2 files changed

Lines changed: 16 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,19 @@ def test_semitransparent_axes_background_preserved():
235235
assert plotly_fig.layout.plot_bgcolor == "rgba(26, 51, 76, 0.4)"
236236

237237

238+
def test_histogram_converts():
239+
"""Histograms must convert without error and keep bargap in plotly's
240+
valid [0, 1] range; get_bar_gap can return a gap with floating point
241+
noise for touching bars, which plotly rejects."""
242+
fig, ax = plt.subplots()
243+
ax.hist(np.random.randn(1000), 30)
244+
245+
plotly_fig = tls.mpl_to_plotly(fig)
246+
247+
assert len(plotly_fig.data) == 1
248+
assert 0 <= plotly_fig.layout.bargap <= 1
249+
250+
238251
def test_line_color_is_valid_plotly_color():
239252
"""Converted line colors are valid plotly color strings: plotly rejects
240253
a space between 'rgba' and the opening parenthesis."""

0 commit comments

Comments
 (0)