Skip to content

Loss of precision with scattergl #6636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
davidlhw opened this issue Jun 9, 2023 · 3 comments
Open

Loss of precision with scattergl #6636

davidlhw opened this issue Jun 9, 2023 · 3 comments
Labels
bug something broken P3 backlog

Comments

@davidlhw
Copy link

davidlhw commented Jun 9, 2023

Codepen link: https://codepen.io/DavidLHW/pen/MWzYXWK

In the above link, with small enough increments (in this case, at 1e-14), the plot will suffer a loss of precision if it's plotted using the scattergl type plot. Notice that the values are all clustered on the far left of the chart:

image

If plot type is changed to scatter, the data would then be plotted correctly like:

image

@nodakai
Copy link

nodakai commented Sep 23, 2023

@alexcjohnson Could you please take a moment to look into this issue? Unlike #5346, I've found that the issue of precision loss is specific to the markers component when using lines+markers with scattergl.

I'm using https://github.com/plotly/Plotly.NET for plotting time-series data that requires nearly microsecond-level resolution. With Unix timestamps currently hovering around ~1.7e9, IEEE-754 doubles serve my needs well, unlike IEEE-754 singles.

For my particular use case, I've found an effective workaround: I'm rendering high-frequency data streams via WebGL lines and overlaying less frequent, yet business-critical, "events" using non-WebGL markers. However, I recognize this approach may not be universally applicable.

@justinjhendrick
Copy link

justinjhendrick commented Nov 10, 2023

I'm seeing a similar issue, but my x axis values are supposed to be integers. It seems that they're being converted to single-precision floats. Is it possible to force plotly and WebGL to keep the x-axis as integers to avoid the loss of precision?

from plotly.io import write_html
from plotly import graph_objs as go

##### Reproduction instructions:
# Use Python 3.11.6
# Use plotly 5.17.0
# Run this script with no arguments. It creates two Plotly HTML files in the working directory
# Open both HTML files in a web browser
#
##### Expected Behavior
# Red point locations in "with_scattergl.html" should be the same as "with_scatter.html"
#
##### Actual Behavior
# Red point locations in "with_scattergl.html" are incorrect.
# They are vertically stacked on the left side of the plot.


def plot(x, y, scatter, file_name):
    fig = go.Figure(
        data=scatter(
            x=x,
            y=y,
            mode="lines+markers",
            marker=dict(
                size=10,
                color="red",
            ),
            line=dict(
                color="blue",
            ),
        ),
        layout=dict(
            title=file_name,
        ),
    )
    with open(file_name, mode="w") as f:
        write_html(fig=fig, file=f)


def main():
    x = [
        1651196752467162363,
        1651196752469163377,
        1651196752569163988,
        1651196752669163486,
        1651196752769162766,
    ]
    y = [0, 1, 0, 2, 0]
    plot(x, y, go.Scattergl, "with_scattergl.html")
    plot(x, y, go.Scatter, "with_scatter.html")


if __name__ == "__main__":
    main()

Screenshot 2023-11-10 at 2 36 48 PM

@justinjhendrick
Copy link

The issue I pointed out in my previous comment is actually M1 Mac specific. See #6820

@gvwilson gvwilson self-assigned this Jul 12, 2024
@gvwilson gvwilson removed their assignment Aug 2, 2024
@gvwilson gvwilson added bug something broken P3 backlog labels Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken P3 backlog
Projects
None yet
Development

No branches or pull requests

4 participants