Skip to content

Commit fc226f7

Browse files
authored
legend items example (#2108)
1 parent 5d5e5d8 commit fc226f7

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

Diff for: doc/python/3d-volume.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.2.3
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -43,7 +43,7 @@ In the three examples below, note that the default colormap is different whether
4343
import plotly.graph_objects as go
4444
import numpy as np
4545
X, Y, Z = np.mgrid[-8:8:40j, -8:8:40j, -8:8:40j]
46-
values = np.sin(X*Y*Z) / (X*Y*Z)
46+
values = np.sin(X*Y*Z) / (X*Y*Z)
4747

4848
fig = go.Figure(data=go.Volume(
4949
x=X.flatten(),

Diff for: doc/python/cone-plot.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.2.3
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python

Diff for: doc/python/legend.md

+44
Original file line numberDiff line numberDiff line change
@@ -360,5 +360,49 @@ fig.add_trace(go.Scatter(
360360
fig.show()
361361
```
362362

363+
### Legend items for continuous fields (2D and 3D)
364+
365+
Traces corresponding to 2D fields (e.g. `go.Heatmap`, `go.Histogram2d`) or 3D fields (e.g. `go.Isosurface`, `go.Volume`, `go.Cone`) can also appear in the legend. They come with legend icons corresponding to each trace type, which are colored using the same colorscale as the trace.
366+
367+
The example below explores a vector field using several traces. Note that you can click on legend items to hide or to select (with a double click) a specific trace. This will make the exploration of your data easier!
368+
369+
```python
370+
import numpy as np
371+
import plotly.graph_objects as go
372+
373+
# Define vector and scalar fields
374+
x, y, z = np.mgrid[0:1:8j, 0:1:8j, 0:1:8j]
375+
u = np.sin(np.pi*x) * np.cos(np.pi*z)
376+
v = -2*np.sin(np.pi*y) * np.cos(2*np.pi*z)
377+
w = np.cos(np.pi*x)*np.sin(np.pi*z) + np.cos(np.pi*y)*np.sin(2*np.pi*z)
378+
magnitude = np.sqrt(u**2 + v**2 + w**2)
379+
mask1 = np.logical_and(y>=.4, y<=.6)
380+
mask2 = y>.6
381+
382+
fig = go.Figure(go.Isosurface(
383+
x=x.ravel(), y=y.ravel(), z=z.ravel(),
384+
value=magnitude.ravel(),
385+
isomin=1.9, isomax=1.9,
386+
colorscale="BuGn",
387+
name='isosurface'))
388+
389+
390+
fig.add_trace(go.Cone(x=x[mask1], y=y[mask1], z=z[mask1],
391+
u=u[mask1], v=v[mask1], w=w[mask1],
392+
colorscale="Blues",
393+
name='cones'
394+
))
395+
fig.add_trace(go.Streamtube(
396+
x=x[mask2], y=y[mask2], z=z[mask2],
397+
u=u[mask2], v=v[mask2], w=w[mask2],
398+
colorscale="Reds",
399+
name='streamtubes'
400+
))
401+
# Update all traces together
402+
fig.update_traces(showlegend=True, showscale=False)
403+
fig.update_layout(width=600, title_text='Exporation of a vector field using several traces')
404+
fig.show()
405+
```
406+
363407
#### Reference
364408
See https://plot.ly/python/reference/#layout-legend for more information!

Diff for: doc/python/streamtube-plot.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: '1.1'
9-
jupytext_version: 1.2.3
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -110,9 +110,9 @@ from plotly.subplots import make_subplots
110110
import numpy as np
111111

112112
x, y, z = np.mgrid[0:10, 0:10, 0:10]
113-
x = x.T.flatten()
114-
y = y.T.flatten()
115-
z = z.T.flatten()
113+
x = x.flatten()
114+
y = y.flatten()
115+
z = z.flatten()
116116

117117
u = np.zeros_like(x)
118118
v = np.zeros_like(y)

0 commit comments

Comments
 (0)