SVG and canvas/WebGL: two browser capabilities for rendering¶
plotly figures are rendered by web browsers, which broadly speaking have two families of capabilities for rendering graphics: the SVG API which supports vector rendering, and the Canvas API which supports raster rendering, and can exploit GPU hardware acceleration via a browser technology known as WebGL. Each
plotly trace type is primarily rendered with either SVG or WebGL, although WebGL-powered traces also use some SVG. The following trace types use WebGL for part or all of the rendering:
- Accelerated versions of SVG trace types:
- High-performance multidimensional trace types:
- 3-d trace types
- Mapbox Gl JS-powered trace types:
WebGL Limitations and Tradeoffs¶
WebGL is a powerful technology for accelerating computation but comes with some strict limitations:
- GPU requirement: WebGL is a GPU (graphics card) technology and therefore requires specific hardware which is available in most but not all cases and is supported by most but not all browsers
- Rasterization: WebGL-rendered data is drawn as a grid of pixels rather than as individual shapes, so can appear pixelated or fuzz in certain cases, and when exported to static file formats will appear pixelated on zoom. In addition: text rendering will differ between SVG and WebGL-powered traces.
- Context limits: browsers impose a strict limit on the number of WebGL "contexts" that any given web document can access. WebGL-powered traces in
plotlycan use multiple contexts in some cases but as a general rule, it may not be possible to render more than 8 WebGL-involving figures on the same page at the same time.
- Size limits: browsers impose hardware-dependent limits on the height and width of figures using WebGL which users may encounter with extremely large plots (e.g. tens of thousands of pixels of height)
In addition to the above limitations, the WebGL-powered version of certain SVG-powered trace types (
heatmapgl) are not complete drop-in replacements for their SVG counterparts yet
- Available symbols will differ
- Area fills are not yet supported in WebGL
- Range breaks on time-series axes are not yet supported
- Axis range heuristics may differ
WebGL for Scatter Performance¶
In the examples below we show that it is possible to represent up to around a million points with WebGL-enabled traces. For larger datasets, or for a clearer visualization of the density of points, it is also possible to use datashader.
WebGL with Plotly Express¶
rendermode argument to supported Plotly Express functions (e.g.
scatter_polar) can be used to enable WebGL rendering.
Note The default
"auto", in which case Plotly Express will automatically set
rendermode="webgl"if the input data is more than 1,000 rows long. If WebGL acceleration is not desired in this case,
rendermodecan be forced to
"svg"for vectorized, if slower, rendering.
Here is an example that creates a 100,000 point scatter plot using Plotly Express with WebGL rendering explicitly enabled.
import plotly.express as px import pandas as pd import numpy as np np.random.seed(1) N = 100000 df = pd.DataFrame(dict(x=np.random.randn(N), y=np.random.randn(N))) fig = px.scatter(df, x="x", y="y", render_mode='webgl') fig.update_traces(marker_line=dict(width=1, color='DarkSlateGray')) fig.show()