Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eb30518

Browse files
committedSep 19, 2019
streamlines and streamtube
1 parent 0402d00 commit eb30518

File tree

3 files changed

+146
-312
lines changed

3 files changed

+146
-312
lines changed
 

‎python/streamline-plots.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.1'
9+
jupytext_version: 1.1.1
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.7.3
24+
plotly:
25+
description: How to make a streamline plot in Python. A streamline plot displays
26+
vector field data.
27+
display_as: scientific
28+
has_thumbnail: true
29+
ipynb: ~notebook_demo/43
30+
language: python
31+
layout: user-guide
32+
name: Streamline Plots
33+
order: 13
34+
permalink: python/streamline-plots/
35+
thumbnail: thumbnail/streamline.jpg
36+
title: Python Streamline Plots | plotly
37+
---
38+
39+
A Streamline plot is a representation based on a 2-D vector field interpreted as a velocity field, consisting of closed curves tangent to the velocity field. In the case of a stationary velocity field, streamlines coincide with trajectories (see also the [Wikipedia page on streamlines, streaklines and pathlines](https://en.wikipedia.org/wiki/Streamlines,_streaklines,_and_pathlines)).
40+
41+
For the streamline figure factory, one needs to provide
42+
- uniformly spaced ranges of `x` and `y` values (1D)
43+
- 2-D velocity values `u` and `v` defined on the cross-product (`np.meshgrid(x, y)`) of `x` and `y`.
44+
45+
Velocity values are interpolated when determining the streamlines. Streamlines are initialized on the boundary of the `x-y` domain.
46+
47+
#### Basic Streamline Plot
48+
49+
```python
50+
import plotly.figure_factory as ff
51+
52+
import numpy as np
53+
54+
x = np.linspace(-3, 3, 100)
55+
y = np.linspace(-3, 3, 100)
56+
Y, X = np.meshgrid(x, y)
57+
u = -1 - X**2 + Y
58+
v = 1 + X - Y**2
59+
60+
# Create streamline figure
61+
fig = ff.create_streamline(x, y, u, v, arrow_scale=.1)
62+
fig.show()
63+
```
64+
65+
#### Streamline and Source Point Plot
66+
67+
```python
68+
import plotly.figure_factory as ff
69+
import plotly.graph_objects as go
70+
71+
import numpy as np
72+
73+
N = 50
74+
x_start, x_end = -2.0, 2.0
75+
y_start, y_end = -1.0, 1.0
76+
x = np.linspace(x_start, x_end, N)
77+
y = np.linspace(y_start, y_end, N)
78+
X, Y = np.meshgrid(x, y)
79+
source_strength = 5.0
80+
x_source, y_source = -1.0, 0.0
81+
82+
# Compute the velocity field on the mesh grid
83+
u = (source_strength/(2*np.pi) *
84+
(X - x_source)/((X - x_source)**2 + (Y - y_source)**2))
85+
v = (source_strength/(2*np.pi) *
86+
(Y - y_source)/((X - x_source)**2 + (Y - y_source)**2))
87+
88+
# Create streamline figure
89+
fig = ff.create_streamline(x, y, u, v,
90+
name='streamline')
91+
92+
# Add source point
93+
fig.add_trace(go.Scatter(x=[x_source], y=[y_source],
94+
mode='markers',
95+
marker_size=14,
96+
name='source point'))
97+
98+
fig.show()
99+
```
100+
101+
#### See also
102+
103+
For a 3D version of streamlines, use the trace `go.Streamtube` documented [here](./streamtube-plot/).
104+
105+
For representing the 2-D vector field as arrows, see the [quiver plot tutorial](./quiver-plots/).
106+
107+
108+
#### Reference
109+
110+
```python
111+
help(ff.create_streamline)
112+
```

‎python/streamtube-plot.md

Lines changed: 34 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -40,195 +40,23 @@ jupyter:
4040
### Introduction
4141

4242

43-
In streamtube plots, attributes inlcude `x`, `y`, and `z`, which set the coordinates of the vector field, and `u`, `v`, and `w`, which sets the x, y, and z components of the vector field. Additionally, you can use `starts` to determine the streamtube's starting position. Lastly, `maxdisplayed` determines the maximum segments displayed in a streamtube.
43+
In streamtube plots, attributes include `x`, `y`, and `z`, which set the coordinates of the vector field, and `u`, `v`, and `w`, which set the x, y, and z components of the vector field. Additionally, you can use `starts` to determine the streamtube's starting position.
4444

4545

4646
### Basic Streamtube Plot
4747

4848
```python
4949
import plotly.graph_objects as go
5050

51-
import pandas as pd
52-
53-
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/streamtube-basic.csv')
54-
55-
fig = go.Figure(data=go.Streamtube(
56-
x = df['x'],
57-
y = df['y'],
58-
z = df['z'],
59-
u = df['u'],
60-
v = df['v'],
61-
w = df['w'],
62-
sizeref = 0.5,
63-
colorscale = 'Blues',
64-
cmin = 0,
65-
cmax = 3
66-
))
67-
68-
69-
fig.update_layout(
70-
scene = dict(
71-
camera = dict(
72-
eye = dict(
73-
x = -0.7243612458865182,
74-
y = 1.9269804254717962,
75-
z = 0.6704828299861716
76-
)
77-
)
78-
)
79-
)
80-
81-
fig.show()
82-
```
83-
84-
```python
85-
import plotly.graph_objects as go
86-
87-
fig = go.Figure(data=go.Streamtube(x=[0, 0, 0], y=[0, 1, 2], z=[0, 0, 0], u=[0, 0, 0], v=[1, 2, 3], w=[0, 0, 0]))
51+
fig = go.Figure(data=go.Streamtube(x=[0, 0, 0], y=[0, 1, 2], z=[0, 0, 0],
52+
u=[0, 0, 0], v=[1, 1, 1], w=[0, 0, 0]))
8853
fig.show()
8954
```
9055

91-
```python
92-
import plotly.graph_objects as go
93-
94-
fig = go.Figure(data=go.Streamtube(x=[0, 1, 2], y=[0, 0, 0], z=[0, 0, 0], u=[1, 2, 3], v=[0, 0, 0], w=[0, 0, 0],
95-
starts=dict(x=[0],
96-
y=[0],
97-
z=[0],)))
98-
fig.show()
99-
```
100-
101-
```python
102-
import plotly.graph_objects as go
103-
104-
fig = go.Figure(data=go.Streamtube(x=[0, 0, 0], y=[0, 0, 0], z=[0, 1, 2], u=[0, 0, 0], v=[0, 0, 0], w=[1, 2, 3],
105-
starts=dict(x=[0],
106-
y=[0],
107-
z=[0],)))
108-
fig.show()
109-
```
110-
111-
```python
112-
import numpy as np
113-
import plotly.graph_objects as go
114-
115-
x, y, z = np.mgrid[1:4:10j, 1:4:10j, 1:4:10j]
116-
u = 0.001 * x
117-
v = 0.001 * y
118-
w = 0.1 * z
119-
120-
sx=np.linspace(1, 4, 10, endpoint=True)
121-
sx, sy=np.meshgrid(sx, sx)
122-
sz=np.ones(sx.shape)
123-
124-
125-
fig = go.Figure(data=go.Streamtube(
126-
x=x.ravel(),
127-
y=y.ravel(),
128-
z=z.ravel(),
129-
u=u.ravel(),
130-
v=v.ravel(),
131-
w=w.ravel(),
132-
starts=dict(x=sx.flatten(),
133-
y=sy.flatten(),
134-
z=sz.flatten()),
135-
hoverinfo='x+y+z+u+v+w+norm',
136-
#sizeref=0.3
137-
))
138-
fig.show()
139-
```
140-
141-
```python
142-
import numpy as np
143-
import plotly.graph_objects as go
144-
145-
x, y, z = np.mgrid[1:4:10j, 1:4:10j, 1:4:10j]
146-
u = 0.000 * x**2
147-
v = 0.00 * np.random.random(y.shape)
148-
w = 0.1 * y
149-
150-
sx=np.linspace(1, 4, 10, endpoint=True)
151-
sx, sy=np.meshgrid(sx, sx)
152-
sz=np.ones(sx.shape)
153-
154-
155-
fig = go.Figure(data=go.Streamtube(
156-
x=x.ravel(),
157-
y=y.ravel(),
158-
z=z.ravel(),
159-
u=u.ravel(),
160-
v=v.ravel(),
161-
w=w.ravel(),
162-
starts=dict(x=sx.flatten(),
163-
y=sy.flatten(),
164-
z=sz.flatten()),
165-
hoverinfo='x+y+z+u+v+w+norm',
166-
sizeref=0.3
167-
))
168-
fig.show()
169-
```
170-
171-
```python
172-
import numpy as np
173-
import plotly.graph_objects as go
174-
175-
x, y, z = np.mgrid[1:2:4j, 1:2:4j, 1:2:4j]
176-
u = np.zeros_like(x)
177-
v = x.copy()
178-
w = np.zeros_like(z)
179-
180-
fig = go.Figure(data=go.Streamtube(x=x.ravel(), y=y.ravel(), z=z.ravel(),
181-
u=u.ravel(), v=v.ravel(), w=w.ravel(),
182-
hoverinfo=['x', 'y']
183-
))
184-
fig.show()
185-
```
186-
187-
```python
188-
x.ravel(), y.ravel(), z.ravel(), v.ravel()
189-
```
190-
191-
```python
192-
v.ravel()
193-
```
194-
195-
```
196-
fig.data
197-
198-
```
199-
200-
```python
201-
import numpy as np
202-
import plotly.graph_objects as go
203-
204-
x, y, z = np.mgrid[-4:4:20j, -4:4:20j, 0:4:20j]
205-
r = np.sqrt(x ** 2 + y ** 2 + z ** 2 + 0.1)
206-
#u = 0.1 * y * np.sin(r) / r
207-
u = 0.005 * np.random.random(x.shape)
208-
v = 0.005 * np.random.random(y.shape)
209-
#v = -0.1 * x * np.sin(r) / r
210-
w = 0.1 * z
211-
212-
sx=np.linspace(-4, 4, 10)
213-
sx, sy=np.meshgrid(sx, sx)
214-
sz=np.ones(sx.shape)
215-
216-
217-
fig = go.Figure(data=go.Streamtube(
218-
x=x.ravel(), y=y.ravel(), z=z.ravel(), u=u.ravel(), v=v.ravel(), w=w.ravel(),
219-
starts=dict(x=sx.flatten(),
220-
y=sy.flatten(),
221-
z=sz.flatten()),
222-
))
223-
fig.show()
224-
```
225-
226-
```python
227-
df
228-
```
229-
23056
### Starting Position and Segments
23157

58+
By default, streamlines are initialized in the x-z plane of minimal y value. You can change this behaviour by providing directly the starting points of streamtubes.
59+
23260
```python
23361
import plotly.graph_objects as go
23462

@@ -273,8 +101,36 @@ fig.update_layout(
273101
fig.show()
274102
```
275103

104+
### Tube color and diameter
105+
106+
The color of tubes is determined by their local norm, and the diameter of the field by the local [divergence](https://en.wikipedia.org/wiki/Divergence) of the vector field.
107+
108+
In all cases below the norm is proportional to `z**2` but the direction of the vector is different, resulting in a different divergence field.
109+
276110
```python
277-
df
111+
import plotly.graph_objects as go
112+
from plotly.subplots import make_subplots
113+
import numpy as np
114+
115+
x, y, z = np.mgrid[0:10, 0:10, 0:10]
116+
x = x.T.flatten()
117+
y = y.T.flatten()
118+
z = z.T.flatten()
119+
120+
u = np.zeros_like(x)
121+
v = np.zeros_like(y)
122+
w = z**2
123+
124+
fig = make_subplots(rows=1, cols=3, specs=[[{'is_3d': True}, {'is_3d': True}, {'is_3d':True}]])
125+
126+
fig.add_trace(go.Streamtube(x=x, y=y, z=z, u=u, v=v, w=w), 1, 1)
127+
fig.add_trace(go.Streamtube(x=x, y=y, z=z, u=w, v=v, w=u), 1, 2)
128+
fig.add_trace(go.Streamtube(x=x, y=y, z=z, u=u, v=w, w=v), 1, 3)
129+
130+
fig.update_layout(scene_camera_eye=dict(x=2, y=2, z=2),
131+
scene2_camera_eye=dict(x=2, y=2, z=2),
132+
scene3_camera_eye=dict(x=2, y=2, z=2))
133+
fig.show()
278134
```
279135

280136
#### Reference

‎unconverted/python/streamline-plots.md

Lines changed: 0 additions & 134 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.