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)
Comment on lines +272 to +274

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to normalize gap0 to calculate the correct bargap.

Suggested change
# 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)
# Plotly's `bargap` is a fraction of the distance between bar positions, not a gap in data units
# so we need to normalize `gap0` with `bar_delta`
bar_delta = bar_starts[1] - bar_starts[0]
if bar_delta <= 0:
return None
# Clamp to guard against floating point noise, such as -8.9e-16 for touching bars
return min(max(gap0 / bar_delta, 0.0), 1.0)



def convert_rgba_array(color_list):
Expand Down
13 changes: 13 additions & 0 deletions plotly/matplotlylib/tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,19 @@ def test_semitransparent_axes_background_preserved():
assert plotly_fig.layout.plot_bgcolor == "rgba(26, 51, 76, 0.4)"


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
Comment on lines +242 to +248

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this test deterministic:

Suggested change
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
# Seed 0 makes the first gap slightly negative (-4.4e-16)
rng = np.random.RandomState(0)
fig, ax = plt.subplots()
ax.hist(rng.randn(10000), 30)
plotly_fig = tls.mpl_to_plotly(fig)
assert len(plotly_fig.data) == 1
assert plotly_fig.layout.bargap == 0



def test_line_color_is_valid_plotly_color():
"""Converted line colors are valid plotly color strings: plotly rejects
a space between 'rgba' and the opening parenthesis."""
Expand Down
Loading