Skip to content

Commit b8e8088

Browse files
authored
Streamtube and quiver tutorials (#124)
1 parent bd66ca0 commit b8e8088

File tree

4 files changed

+250
-294
lines changed

4 files changed

+250
-294
lines changed

python/streamline-plots.md

+112
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

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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.2.3
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 3D streamtube plots in Python with Plotly.
26+
display_as: 3d_charts
27+
has_thumbnail: true
28+
ipynb: ~notebook_demo/207
29+
language: python
30+
layout: user-guide
31+
name: 3D Streamtube Plots
32+
order: 21
33+
page_type: u-guide
34+
permalink: python/streamtube-plot/
35+
thumbnail: thumbnail/streamtube.jpg
36+
title: 3D Streamtube Plots | Plotly
37+
---
38+
39+
40+
### Introduction
41+
42+
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.
44+
45+
46+
### Basic Streamtube Plot
47+
48+
```python
49+
import plotly.graph_objects as go
50+
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]))
53+
fig.show()
54+
```
55+
56+
### Starting Position and Segments
57+
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+
60+
```python
61+
import plotly.graph_objects as go
62+
63+
import pandas as pd
64+
65+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/streamtube-wind.csv').drop(['Unnamed: 0'],axis=1)
66+
67+
fig = go.Figure(data=go.Streamtube(
68+
x = df['x'],
69+
y = df['y'],
70+
z = df['z'],
71+
u = df['u'],
72+
v = df['v'],
73+
w = df['w'],
74+
starts = dict(
75+
x = [80] * 16,
76+
y = [20,30,40,50] * 4,
77+
z = [0,0,0,0,5,5,5,5,10,10,10,10,15,15,15,15]
78+
),
79+
sizeref = 0.3,
80+
colorscale = 'Portland',
81+
showscale = False,
82+
maxdisplayed = 3000
83+
))
84+
85+
fig.update_layout(
86+
scene = dict(
87+
aspectratio = dict(
88+
x = 2,
89+
y = 1,
90+
z = 0.3
91+
)
92+
),
93+
margin = dict(
94+
t = 20,
95+
b = 20,
96+
l = 20,
97+
r = 20
98+
)
99+
)
100+
101+
fig.show()
102+
```
103+
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+
110+
```python
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()
134+
```
135+
136+
#### Reference
137+
See https://plot.ly/python/reference/#streamtube for more information and chart attribute options!
138+

unconverted/python/streamline-plots.md

-134
This file was deleted.

0 commit comments

Comments
 (0)