Skip to content

Fix flaky matplotlib-to-plotly conversion of touching bars (negative bargap) - #5696

Open
robertoffmoura wants to merge 1 commit into
plotly:mainfrom
robertoffmoura:rm/fix-negative-bargap
Open

Fix flaky matplotlib-to-plotly conversion of touching bars (negative bargap)#5696
robertoffmoura wants to merge 1 commit into
plotly:mainfrom
robertoffmoura:rm/fix-negative-bargap

Conversation

@robertoffmoura

Copy link
Copy Markdown
Contributor

mpl_to_plotly intermittently crashes when converting figures with touching bars (e.g. plt.hist). The bar gap is computed as bar_start[i+1] - bar_end[i], and for adjacent bars floating-point noise can make this a tiny negative number (e.g. -8.88e-16). Plotly's bargap property only accepts values in [0, 1], so the conversion fails with:

ValueError: Invalid value of type 'numpy.float64' received for the 'bargap' property of layout
    Received value: np.float64(-8.881784197001252e-16)

Reproduces with:

import warnings
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import plotly.tools as tls

np.random.seed(0)
plt.hist(np.random.randn(10000), 30)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    tls.mpl_to_plotly(plt.gcf())

Fix: get_bar_gap now clamps the gap to [0, 1], so float noise around zero maps to bargap = 0 (touching bars) and genuine gaps are unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant