plotly.js: Plotly fails when using "scattergl" and value greater than 2^24 [Only on M1]
When plotting using Plotly.newplot() and type scattergl on M1 machine, the plotting fails if the value of x-axis is greater than 2^24. Example:
var data = [
{
x: [16777217],
y: [1],
type: "scattergl",
}
];
Plotly.newPlot('myDiv', data);
The data gets plotted on the margin which cannot be zoomed/panned. (16777217 is 2^24 + 1) Attached screen shot. The issue only happens on M1 machine. On Intel hardware everything works well.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 16 (4 by maintainers)
In response to point 1 from my earlier comment, I went looking for other affected WebGL trace types in addition to scattergl. I couldn’t find any others that are affected by this same bug. I checked the following trace types:
I found precision issues generally with x-values near 1e9. Some of them seem like the integers are being converted to float32s and we’re losing precision that way (similar to the scatter3d example from this comment). But I couldn’t find any issues that were M1 Mac specific.
Somewhat surprisingly, splom was not fixed by this change, despite also depending on regl-scatter2d. But I can repro that on Windows, so I guess that’s being caused by something else.
Can anyone else find M1 mac specific precision issues that aren’t fixed by https://github.com/gl-vis/regl-scatter2d/pull/36? If not, I recommend closing this ticket and tracking down the non-platform-specific precision bugs in another issue.
@justinjhendrick we also maintain
gl-vis
modules. Let’s start by the minimal PR toregl-scatter2d
and provide a link to this issue in its description. After that we will could have a look at rendering times on CircleCI, etc. But at this point I’m not too worried about the performance side effect. Thank you!Safari’s implementation of WebGL passes the
-ffast-math
option to the Metal compiler and this is causing the vertex shader to be rewritten from (just concentrating on the x coord of the vertex position):to:
This change in the order of floating-point operations leads to a difference in the calculation causing an incorrect position:
The position and translation
16777216.0
are exactly representable because they’re power-of-2 (2 ** 24), but a 32-bit float can’t represent16777217.0
, so16777216.0 + 1.0
becomes16777216.0
.Which is the incorrect position shown in the initial bug report.
As a workaround,
-ffast-math
can be disabled by markinggl_Position
asinvariant
in the GLSL shader, but it should be noted that this comes with a potential impact on the performance of the shader.I can confirm that @djg’s suggestion fixed the issue for me. I added
invariant gl_Position;
after these two lines in these shaders: regl-scatter2d/marker-vert.glsl and regl-scatter2d/marker-vert.glsl. With those changed,scattergl
was plotting points in the correct places with x values near 1e9.Same issue here for M1 Pro chip + Chrome. Everything’s fine using devices without the new M1 chip. M1
Other devices

The same issue here. This issue happens only when using the
WebGL
mode. The plot becomes normal when manually switched back to theSVG
mode.