|
| 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 3D Isosurface Plots in Python with Plotly. |
| 26 | + display_as: 3d_charts |
| 27 | + has_thumbnail: true |
| 28 | + ipynb: ~notebook_demo/272 |
| 29 | + language: python |
| 30 | + layout: user-guide |
| 31 | + name: 3D Isosurface Plots |
| 32 | + order: 12.1 |
| 33 | + page_type: u-guide |
| 34 | + permalink: python/3d-isosurface-plots/ |
| 35 | + redirect_from: python/isosurfaces-with-marching-cubes/ |
| 36 | + thumbnail: thumbnail/isosurface.jpg |
| 37 | + title: Python 3D Isosurface Plots | plotly |
| 38 | +--- |
| 39 | + |
| 40 | +With ``go.Isosurface``, you can plot [isosurface contours](https://en.wikipedia.org/wiki/Isosurface) of a scalar field ``value``, which is defined on ``x``, ``y`` and ``z`` coordinates. |
| 41 | + |
| 42 | +#### Basic Isosurface |
| 43 | + |
| 44 | +In this first example, we plot the isocontours of values ``isomin=2`` and ``isomax=6``. In addition, portions of the sides of the coordinate domains for which the value is between ``isomin`` and ``isomax`` (named the ``caps``) are colored. Please rotate the figure to visualize both the internal surfaces and the caps surfaces on the sides. |
| 45 | + |
| 46 | +```python |
| 47 | +import plotly.graph_objects as go |
| 48 | + |
| 49 | +fig= go.Figure(data=go.Isosurface( |
| 50 | + x=[0,0,0,0,1,1,1,1], |
| 51 | + y=[1,0,1,0,1,0,1,0], |
| 52 | + z=[1,1,0,0,1,1,0,0], |
| 53 | + value=[1,2,3,4,5,6,7,8], |
| 54 | + isomin=2, |
| 55 | + isomax=6 |
| 56 | +)) |
| 57 | + |
| 58 | +fig.show() |
| 59 | +``` |
| 60 | + |
| 61 | +### Removing caps when visualizing isosurfaces |
| 62 | + |
| 63 | +For a clearer visualization of internal surfaces, it is possible to remove the caps (color-coded surfaces on the sides of the visualization domain). Caps are visible by default. |
| 64 | + |
| 65 | +```python |
| 66 | +import plotly.graph_objects as go |
| 67 | +import numpy as np |
| 68 | + |
| 69 | +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j] |
| 70 | + |
| 71 | +# ellipsoid |
| 72 | +values = X * X * 0.5 + Y * Y + Z * Z * 2 |
| 73 | + |
| 74 | +fig = go.Figure(data=go.Isosurface( |
| 75 | + x=X.flatten(), |
| 76 | + y=Y.flatten(), |
| 77 | + z=Z.flatten(), |
| 78 | + value=values.flatten(), |
| 79 | + isomin=10, |
| 80 | + isomax=40, |
| 81 | + caps=dict(x_show=False, y_show=False) |
| 82 | + )) |
| 83 | +fig.show() |
| 84 | +``` |
| 85 | + |
| 86 | +### Modifying the number of isosurfaces |
| 87 | + |
| 88 | +```python |
| 89 | +import plotly.graph_objects as go |
| 90 | +import numpy as np |
| 91 | + |
| 92 | +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j] |
| 93 | + |
| 94 | +# ellipsoid |
| 95 | +values = X * X * 0.5 + Y * Y + Z * Z * 2 |
| 96 | + |
| 97 | +fig = go.Figure(data=go.Isosurface( |
| 98 | + x=X.flatten(), |
| 99 | + y=Y.flatten(), |
| 100 | + z=Z.flatten(), |
| 101 | + value=values.flatten(), |
| 102 | + isomin=10, |
| 103 | + isomax=50, |
| 104 | + surface_count=5, # number of isosurfaces, 2 by default: only min and max |
| 105 | + colorbar_nticks=5, # colorbar ticks correspond to isosurface values |
| 106 | + caps=dict(x_show=False, y_show=False) |
| 107 | + )) |
| 108 | +fig.show() |
| 109 | +``` |
| 110 | + |
| 111 | +#### Isosurface with Addtional Slices |
| 112 | + |
| 113 | +Here we visualize slices parallel to the axes on top of isosurfaces. For a clearer visualization, the `fill` ratio of isosurfaces is decreased below 1 (completely filled). |
| 114 | + |
| 115 | +```python |
| 116 | +import plotly.graph_objects as go |
| 117 | +import numpy as np |
| 118 | + |
| 119 | +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j] |
| 120 | + |
| 121 | +# ellipsoid |
| 122 | +values = X * X * 0.5 + Y * Y + Z * Z * 2 |
| 123 | + |
| 124 | +fig = go.Figure(data=go.Isosurface( |
| 125 | + x=X.flatten(), |
| 126 | + y=Y.flatten(), |
| 127 | + z=Z.flatten(), |
| 128 | + value=values.flatten(), |
| 129 | + isomin=5, |
| 130 | + isomax=50, |
| 131 | + surface_fill=0.4, |
| 132 | + caps=dict(x_show=False, y_show=False), |
| 133 | + slices_z=dict(show=True, locations=[-1, -3,]), |
| 134 | + slices_y=dict(show=True, locations=[0]), |
| 135 | + )) |
| 136 | +fig.show() |
| 137 | +``` |
| 138 | + |
| 139 | +#### Multiple Isosurfaces with Caps |
| 140 | + |
| 141 | +```python |
| 142 | +import plotly.graph_objects as go |
| 143 | +import numpy as np |
| 144 | + |
| 145 | +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, 0:5:20j] |
| 146 | + |
| 147 | +values = X * X * 0.5 + Y * Y + Z * Z * 2 |
| 148 | + |
| 149 | +fig = go.Figure(data=go.Isosurface( |
| 150 | + x=X.flatten(), |
| 151 | + y=Y.flatten(), |
| 152 | + z=Z.flatten(), |
| 153 | + value=values.flatten(), |
| 154 | + isomin=30, |
| 155 | + isomax=50, |
| 156 | + surface=dict(count=3, fill=0.7, pattern='odd'), |
| 157 | + caps=dict(x_show=True, y_show=True), |
| 158 | + )) |
| 159 | +fig.show() |
| 160 | +``` |
| 161 | + |
| 162 | +### Changing the default colorscale of isosurfaces |
| 163 | + |
| 164 | +```python |
| 165 | +import plotly.graph_objects as go |
| 166 | +import numpy as np |
| 167 | + |
| 168 | +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j] |
| 169 | + |
| 170 | +# ellipsoid |
| 171 | +values = X * X * 0.5 + Y * Y + Z * Z * 2 |
| 172 | + |
| 173 | +fig = go.Figure(data=go.Isosurface( |
| 174 | + x=X.flatten(), |
| 175 | + y=Y.flatten(), |
| 176 | + z=Z.flatten(), |
| 177 | + value=values.flatten(), |
| 178 | + colorscale='BlueRed', |
| 179 | + isomin=10, |
| 180 | + isomax=50, |
| 181 | + surface_count=3, |
| 182 | + caps=dict(x_show=False, y_show=False) |
| 183 | + )) |
| 184 | +fig.show() |
| 185 | +``` |
| 186 | + |
| 187 | +### Customizing the layout and appearance of isosurface plots |
| 188 | + |
| 189 | +```python |
| 190 | +import plotly.graph_objects as go |
| 191 | +import numpy as np |
| 192 | + |
| 193 | +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, 0:5:20j] |
| 194 | + |
| 195 | +values = X * X * 0.5 + Y * Y + Z * Z * 2 |
| 196 | + |
| 197 | +fig = go.Figure(data=go.Isosurface( |
| 198 | + x=X.flatten(), |
| 199 | + y=Y.flatten(), |
| 200 | + z=Z.flatten(), |
| 201 | + value=values.flatten(), |
| 202 | + isomin=30, |
| 203 | + isomax=50, |
| 204 | + surface=dict(count=3, fill=0.7, pattern='odd'), |
| 205 | + showscale=False, # remove colorbar |
| 206 | + caps=dict(x_show=True, y_show=True), |
| 207 | + )) |
| 208 | + |
| 209 | +fig.update_layout( |
| 210 | + margin=dict(t=0, l=0, b=0), # tight layout |
| 211 | + scene_camera_eye=dict(x=1.86, y=0.61, z=0.98)) |
| 212 | +fig.show() |
| 213 | +``` |
| 214 | + |
| 215 | +#### Reference |
| 216 | +See https://plot.ly/python/reference/#isosurface for more information and chart attribute options! |
| 217 | + |
0 commit comments