|
| 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 | + |
0 commit comments