diff --git a/python/3d-isosurface-plots.md b/python/3d-isosurface-plots.md new file mode 100644 index 000000000..f206bcda7 --- /dev/null +++ b/python/3d-isosurface-plots.md @@ -0,0 +1,242 @@ +--- +jupyter: + jupytext: + notebook_metadata_filter: all + text_representation: + extension: .md + format_name: markdown + format_version: '1.1' + jupytext_version: 1.1.1 + kernelspec: + display_name: Python 3 + language: python + name: python3 + language_info: + codemirror_mode: + name: ipython + version: 3 + file_extension: .py + mimetype: text/x-python + name: python + nbconvert_exporter: python + pygments_lexer: ipython3 + version: 3.7.3 + plotly: + description: How to make 3D Isosurface Plots in Python with Plotly. + display_as: 3d_charts + has_thumbnail: true + ipynb: ~notebook_demo/272 + language: python + layout: user-guide + name: 3D Isosurface Plots + order: 12.1 + page_type: u-guide + permalink: python/3d-isosurface-plots/ + redirect_from: python/isosurfaces-with-marching-cubes/ + thumbnail: thumbnail/isosurface.jpg + title: Python 3D Isosurface Plots | plotly +--- + +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. + +#### Basic Isosurface + +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. + +```python +import plotly.graph_objects as go + +fig= go.Figure(data=go.Isosurface( + x=[0,0,0,0,1,1,1,1], + y=[1,0,1,0,1,0,1,0], + z=[1,1,0,0,1,1,0,0], + value=[1,2,3,4,5,6,7,8], + isomin=2, + isomax=6, +)) + +fig.show() +``` + +### Removing caps when visualizing isosurfaces + +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. + +```python +import plotly.graph_objects as go +import numpy as np + +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j] + +# ellipsoid +values = X * X * 0.5 + Y * Y + Z * Z * 2 + +fig = go.Figure(data=go.Isosurface( + x=X.flatten(), + y=Y.flatten(), + z=Z.flatten(), + value=values.flatten(), + isomin=10, + isomax=40, + caps=dict(x_show=False, y_show=False) + )) +fig.show() +``` + +### Modifying the number of isosurfaces + +```python +import plotly.graph_objects as go +import numpy as np + +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j] + +# ellipsoid +values = X * X * 0.5 + Y * Y + Z * Z * 2 + +fig = go.Figure(data=go.Isosurface( + x=X.flatten(), + y=Y.flatten(), + z=Z.flatten(), + value=values.flatten(), + isomin=10, + isomax=50, + surface_count=5, # number of isosurfaces, 2 by default: only min and max + colorbar_nticks=5, # colorbar ticks correspond to isosurface values + caps=dict(x_show=False, y_show=False) + )) +fig.show() +``` + +### Changing the opacity of isosurfaces + +```python +import plotly.graph_objects as go +import numpy as np + +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j] + +# ellipsoid +values = X * X * 0.5 + Y * Y + Z * Z * 2 + +fig = go.Figure(data=go.Isosurface( + x=X.flatten(), + y=Y.flatten(), + z=Z.flatten(), + value=values.flatten(), + opacity=0.6, + isomin=10, + isomax=50, + surface_count=3, + caps=dict(x_show=False, y_show=False) + )) +fig.show() +``` + +#### Isosurface with Addtional Slices + +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). + +```python +import plotly.graph_objects as go +import numpy as np + +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j] + +# ellipsoid +values = X * X * 0.5 + Y * Y + Z * Z * 2 + +fig = go.Figure(data=go.Isosurface( + x=X.flatten(), + y=Y.flatten(), + z=Z.flatten(), + value=values.flatten(), + isomin=5, + isomax=50, + surface_fill=0.4, + caps=dict(x_show=False, y_show=False), + slices_z=dict(show=True, locations=[-1, -3,]), + slices_y=dict(show=True, locations=[0]), + )) +fig.show() +``` + +#### Multiple Isosurfaces with Caps + +```python +import plotly.graph_objects as go +import numpy as np + +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, 0:5:20j] + +values = X * X * 0.5 + Y * Y + Z * Z * 2 + +fig = go.Figure(data=go.Isosurface( + x=X.flatten(), + y=Y.flatten(), + z=Z.flatten(), + value=values.flatten(), + isomin=30, + isomax=50, + surface=dict(count=3, fill=0.7, pattern='odd'), + caps=dict(x_show=True, y_show=True), + )) +fig.show() +``` + +### Changing the default colorscale of isosurfaces + +```python +import plotly.graph_objects as go +import numpy as np + +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, -5:5:40j] + +# ellipsoid +values = X * X * 0.5 + Y * Y + Z * Z * 2 + +fig = go.Figure(data=go.Isosurface( + x=X.flatten(), + y=Y.flatten(), + z=Z.flatten(), + value=values.flatten(), + colorscale='BlueRed', + isomin=10, + isomax=50, + surface_count=3, + caps=dict(x_show=False, y_show=False) + )) +fig.show() +``` + +### Customizing the layout and appearance of isosurface plots + +```python +import plotly.graph_objects as go +import numpy as np + +X, Y, Z = np.mgrid[-5:5:40j, -5:5:40j, 0:5:20j] + +values = X * X * 0.5 + Y * Y + Z * Z * 2 + +fig = go.Figure(data=go.Isosurface( + x=X.flatten(), + y=Y.flatten(), + z=Z.flatten(), + value=values.flatten(), + isomin=30, + isomax=50, + surface=dict(count=3, fill=0.7, pattern='odd'), + showscale=False, # remove colorbar + caps=dict(x_show=True, y_show=True), + )) + +fig.update_layout( + margin=dict(t=0, l=0, b=0), # tight layout + scene_camera_eye=dict(x=1.86, y=0.61, z=0.98)) +fig.show() +``` + +#### Reference +See https://plot.ly/python/reference/#isosurface for more information and chart attribute options! + diff --git a/unconverted/python/3d-isosurface-plots.md b/unconverted/python/3d-isosurface-plots.md deleted file mode 100644 index 3af3ff5d8..000000000 --- a/unconverted/python/3d-isosurface-plots.md +++ /dev/null @@ -1,181 +0,0 @@ ---- -jupyter: - jupytext: - notebook_metadata_filter: all - text_representation: - extension: .md - format_name: markdown - format_version: '1.1' - jupytext_version: 1.1.1 - kernelspec: - display_name: Python 3 - language: python - name: python3 - plotly: - description: How to make 3D Isosurface Plots in Python with Plotly. - display_as: 3d_charts - has_thumbnail: true - ipynb: ~notebook_demo/272 - language: python - layout: user-guide - name: 3D Isosurface Plots - order: 12.1 - page_type: u-guide - permalink: python/3d-isosurface-plots/ - redirect_from: python/isosurfaces-with-marching-cubes/ - thumbnail: thumbnail/isosurface.jpg - title: Python 3D Isosurface Plots | plotly ---- - -#### New to Plotly? -Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/). -
You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online). -
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started! - - -#### Basic Isosurface - -```python -import plotly.plotly as py -import plotly.graph_objs as go - -data = [go.Isosurface( - x=[0,0,0,0,1,1,1,1], - y=[1,0,1,0,1,0,1,0], - z=[1,1,0,0,1,1,0,0], - value=[1,2,3,4,5,6,7,8], - isomin=2, - isomax=6 -)] - -py.iplot(data, filename='basic-isosurface-trace') -``` - -#### Isosurface with Addtional Slices - -```python -import plotly.plotly as py -import plotly.graph_objs as go - -import numpy as np - -f = lambda x, y, z: 81*(x**3 + y**3 + z**3) - 189*(x**2*y + x**2*z + y**2*x +y**2*z + z**2*x + z**2*y) +\ - 54*(x*y*z) + 126*(x*y + x*z + y*z) - 9*(x**2 + y**2 + z**2) - 9*(x + y + z) + 1 - -a = 1 -X, Y, Z = np.mgrid[-a:a:25j, -a:a:25j, -a:a:25j] - -data = [go.Isosurface( - x=X.flatten(), - y=Y.flatten(), - z=Z.flatten(), - value=f(X, Y, Z).flatten(), - isomin=-100, - isomax=100, - surface=dict(show=True,count=1, fill=0.8), - slices=go.isosurface.Slices( - z=go.isosurface.slices.Z( - show = True, - locations=[-0.3, 0.5]) - ), - caps=go.isosurface.Caps( - z=dict(show=False), - x=dict(show=False), - y=dict(show=False) - ), -)] - -layout = go.Layout( - margin=dict(t=0, l=0, b=0), - scene=dict( - camera=dict( - eye=dict( - x=1.86, - y=0.61, - z=0.98 - ) - ) - ) -) - -fig = go.Figure(data, layout) - -py.iplot(fig, config=dict(showSendToCloud=True), filename='isosurface-with-slices') -``` - -#### Multiple Isosurfaces with Caps - -```python -import plotly.plotly as py -import plotly.graph_objs as go -import plotly.io as pio - -import numpy as np - -f = lambda x, y, z: 81*(x**3 + y**3 + z**3) - 189*(x**2*y + x**2*z + y**2*x +y**2*z + z**2*x + z**2*y) +\ - 54*(x*y*z) + 126*(x*y + x*z + y*z) - 9*(x**2 + y**2 + z**2) - 9*(x + y + z) + 1 - -a = 1 -X, Y, Z = np.mgrid[-a:a:25j, -a:a:25j, -a:a:25j] - -data = [go.Isosurface( - x=X.flatten(), - y=Y.flatten(), - z=Z.flatten(), - value=f(X, Y, Z).flatten(), - isomin=-10, - isomax=10, - surface=dict(show=True,count=4, fill=0.8, pattern='odd'), - caps=go.isosurface.Caps( - z=dict(show=True), - x=dict(show=True), - y=dict(show=True) - ), -)] - -layout = go.Layout( - margin=dict(t=0, l=0, b=0), - template=pio.templates['plotly'], - scene=dict( - camera=dict( - eye=dict( - x=1.86, - y=0.61, - z=0.98 - ) - ) - ) -) - -fig = go.Figure(data, layout) - -py.iplot(fig, config=dict(showSendToCloud=True), filename='multiple-isosurface-with-caps') -``` - -#### Reference -See https://plot.ly/python/reference/#isosurface for more information and chart attribute options! - -```python -from IPython.display import display, HTML - -display(HTML('')) -display(HTML('')) - -! pip install git+https://github.com/plotly/publisher.git --upgrade - -import publisher -publisher.publish( - 'isosurfaces.ipynb', 'python/3d-isosurface-plots/', 'Iso Surface', - 'How to make 3D Isosurface Plots in Python with Plotly.', - title = 'Python 3D Isosurface Plots | plotly', - name = '3D Isosurface Plots', - has_thumbnail='true', thumbnail='thumbnail/isosurface.jpg', - redirect_from='python/isosurfaces-with-marching-cubes/', - language='python', - display_as='3d_charts', order=12.1, - ipynb= '~notebook_demo/272') -``` - -```python - -```