You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/python/performance.md
+78-62
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ jupyter:
6
6
extension: .md
7
7
format_name: markdown
8
8
format_version: '1.3'
9
-
jupytext_version: 1.16.1
9
+
jupytext_version: 1.16.4
10
10
kernelspec:
11
11
display_name: Python 3 (ipykernel)
12
12
language: python
@@ -20,23 +20,28 @@ jupyter:
20
20
name: python
21
21
nbconvert_exporter: python
22
22
pygments_lexer: ipython3
23
-
version: 3.10.11
23
+
version: 3.11.10
24
24
plotly:
25
-
description: Using WebGL for increased speed, improved interactivity, and the
26
-
ability to plot even more data!
25
+
description: Recommendations for increased speed, improved interactivity, and
26
+
the ability to plot even more data!
27
27
display_as: basic
28
28
language: python
29
29
layout: base
30
-
name: WebGL vs SVG
30
+
name: High Performance Visualization
31
31
order: 14
32
-
permalink: python/webgl-vs-svg/
33
-
redirect_from: python/compare-webgl-svg/
32
+
permalink: python/performance/
33
+
redirect_from: python/webgl-vs-svg/
34
34
thumbnail: thumbnail/webgl.jpg
35
35
---
36
36
37
-
### SVG and canvas/WebGL: two browser capabilities for rendering
37
+
##WebGL
38
38
39
-
`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:
39
+
`plotly` figures are rendered by web browsers, which broadly speaking have two families of capabilities for rendering graphics:
40
+
41
+
- The SVG API, which supports vector rendering.
42
+
- The Canvas API, which supports raster rendering, and can exploit GPU hardware acceleration via a browser technology known as WebGL.
43
+
44
+
Each `plotly` trace type is rendered with either SVG or WebGL. The following trace types use WebGL for rendering:
40
45
41
46
* Accelerated versions of SVG trace types: `scattergl`, `scatterpolargl`,
42
47
* High-performance multidimensional trace types: `splom`, or `parcoords`
@@ -47,18 +52,18 @@ jupyter:
47
52
48
53
WebGL is a powerful technology for accelerating computation but comes with some strict limitations:
49
54
50
-
1. 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
55
+
1. 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.
51
56
2. 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.
52
-
3. Context limits: browsers impose a strict limit on the number of WebGL "contexts" that any given web document can access. WebGL-powered traces in `plotly` can 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.**
53
-
4. 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)
57
+
3. Context limits: browsers impose a strict limit on the number of WebGL "contexts" that any given web document can access. WebGL-powered traces in `plotly` can 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.** See the following section, Multiple WebGL Contexts, for more details.
58
+
4. 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).
54
59
55
60
In addition to the above limitations, the WebGL-powered version of certain SVG-powered trace types (`scattergl`, `scatterpolargl`) are not complete drop-in replacements for their SVG counterparts yet
56
-
* Available symbols will differ
57
-
* Area fills are not yet supported in WebGL
58
-
* Range breaks on time-series axes are not yet supported
59
-
* Axis range heuristics may differ
61
+
* Available symbols will differ.
62
+
* Area fills are not yet supported in WebGL.
63
+
* Range breaks on time-series axes are not yet supported.
64
+
* Axis range heuristics may differ.
60
65
61
-
### Multiple WebGL Contexts
66
+
####Multiple WebGL Contexts
62
67
63
68
*New in 5.19*
64
69
@@ -72,12 +77,12 @@ To use it, in the environment where your Plotly figures are being rendered, load
72
77
73
78
In a Jupyter notebook environment that supports magic commands, you can load it with the [HTML magic command](https://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-html):
@@ -87,9 +92,9 @@ it is also possible to use [datashader](/python/datashader/).
87
92
88
93
### WebGL with Plotly Express
89
94
90
-
The `rendermode` argument to supported Plotly Express functions (e.g. `scatter` and `scatter_polar`) can be used to enable WebGL rendering.
95
+
The `render_mode` argument to supported Plotly Express functions (e.g. `scatter` and `scatter_polar`) can be used to enable WebGL rendering.
91
96
92
-
> **Note** The default `rendermode` is `"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, `rendermode` can be forced to `"svg"` for vectorized, if slower, rendering.
97
+
> **Note** The default `render_mode` is `"auto"`, in which case Plotly Express will automatically set `render_mode="webgl"` if the input data is more than 1,000 rows long. In this case, WebGl can be disabled by setting `render_mode=svg`.
93
98
94
99
Here is an example that creates a 100,000 point scatter plot using Plotly Express with WebGL rendering explicitly enabled.
95
100
@@ -98,6 +103,7 @@ import plotly.express as px
98
103
99
104
import pandas as pd
100
105
import numpy as np
106
+
101
107
np.random.seed(1)
102
108
103
109
N =100000
@@ -113,7 +119,7 @@ fig.show()
113
119
```
114
120
115
121
116
-
#### WebGL with 100,000 points with Graph Objects
122
+
#### WebGL with 1,000,000 points with Graph Objects
117
123
118
124
If Plotly Express does not provide a good starting point, it is also possible to use [the more generic `go.Scattergl` class from `plotly.graph_objects`](/python/graph-objects/).
119
125
@@ -122,9 +128,8 @@ import plotly.graph_objects as go
122
128
123
129
import numpy as np
124
130
125
-
N =100000
131
+
N =1_000_000
126
132
127
-
# Create figure
128
133
fig = go.Figure()
129
134
130
135
fig.add_trace(
@@ -143,58 +148,69 @@ fig.add_trace(
143
148
fig.show()
144
149
```
145
150
146
-
#### WebGL Rendering with 1 Million Points
151
+
See https://plotly.com/python/reference/scattergl/ for more information and chart attribute options!
147
152
148
-
```python
149
-
import plotly.graph_objects as go
153
+
## NumPy and NumPy Convertible Arrays for Improved Performance
150
154
151
-
import numpy asnp
155
+
Improve the performance of generating Plotly figures that use a large number of data points by using NumPy arrays and other objects that Plotly can convert to NumPy arrays, such as Pandas and Polars Series.
152
156
153
-
N =1000000
157
+
Plotly.py uses Plotly.js for rendering, which supports typed arrays. In Plotly.py, NumPy array and NumPy-convertible arrays are base64 encoded before being passed to Plotly.js for rendering.
154
158
155
-
# Create figure
156
-
fig = go.Figure()
159
+
### Arrays and Data Types Supported
157
160
158
-
fig.add_trace(
159
-
go.Scattergl(
160
-
x= np.random.randn(N),
161
-
y= np.random.randn(N),
162
-
mode='markers',
163
-
marker=dict(
164
-
line=dict(
165
-
width=1,
166
-
color='DarkSlateGrey')
167
-
)
168
-
)
169
-
)
161
+
The following types of objects in Python are supported for improved performance:
- Array objects that can be converted to `numpy.ndarray` objects. i.e., they implement `"__array__"` or `"__array_interface__"` and return a `numpy.ndarray`.
166
+
167
+
The following [array data types](https://numpy.org/devdocs/reference/arrays.scalars.html) are supported:
168
+
169
+
- float32
170
+
- float64
171
+
- int8
172
+
- uint8
173
+
- int16
174
+
- uint16
175
+
- int32
176
+
- uint32
177
+
178
+
*If the array dtype is **int64** and **uint64**, often the default dtype for arrays in NumPy when no dtype is specified, those dtypes will be changed to other types internally by Plotly.py where possible. When working with NumPY directly, you can [specify the `dtype`](https://numpy.org/doc/stable/user/basics.types.html#array-types-and-conversions-between-types) when creating `ndarray` objects.
179
+
180
+
### Unsupported Attributes
181
+
182
+
Arrays passed to attributes with the following names are not supported:
183
+
184
+
`geojson`, `layers`, and `range`.
170
185
171
-
fig.show()
172
-
```
173
186
174
-
#### WebGL with many traces
187
+
### Example with NumPy Arrays
188
+
189
+
Here, we use NumPy arrays with a `go.Scatter3d` figure.
175
190
176
191
```python
177
192
import plotly.graph_objects as go
178
-
179
193
import numpy as np
180
194
181
-
fig = go.Figure()
195
+
np.random.seed(1)
182
196
183
-
trace_num =10
184
-
point_num =5000
185
-
for i inrange(trace_num):
186
-
fig.add_trace(
187
-
go.Scattergl(
188
-
x= np.linspace(0, 1, point_num),
189
-
y= np.random.randn(point_num)+(i*5)
190
-
)
191
-
)
197
+
# Number of data points
198
+
N =10000
199
+
200
+
# Generate random data
201
+
x = np.random.randn(N)
202
+
y = np.random.randn(N).astype('float32')
203
+
z = np.random.randint(size=N, low=0, high=256, dtype='uint8')
204
+
c = np.random.randint(size=N, low=-10, high=10, dtype='int8')
192
205
193
-
fig.update_layout(showlegend=False)
206
+
fig = go.Figure(data=[go.Scatter3d(
207
+
x=x,
208
+
y=y,
209
+
z=z,
210
+
marker=dict(color=c),
211
+
mode='markers',
212
+
opacity=0.2
213
+
)])
194
214
195
215
fig.show()
196
216
```
197
-
198
-
### Reference
199
-
200
-
See https://plotly.com/python/reference/scattergl/ for more information and chart attribute options!
0 commit comments