{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic Isosurface" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "data = [go.Isosurface(\n", " x=[0,0,0,0,1,1,1,1],\n", " y=[1,0,1,0,1,0,1,0],\n", " z=[1,1,0,0,1,1,0,0],\n", " value=[1,2,3,4,5,6,7,8],\n", " isomin=2,\n", " isomax=6\n", ")]\n", "\n", "py.iplot(data, filename='basic-isosurface-trace')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Isosurface with Addtional Slices" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import numpy as np\n", "\n", "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) +\\\n", " 54*(x*y*z) + 126*(x*y + x*z + y*z) - 9*(x**2 + y**2 + z**2) - 9*(x + y + z) + 1\n", "\n", "a = 1\n", "X, Y, Z = np.mgrid[-a:a:25j, -a:a:25j, -a:a:25j]\n", "\n", "data = [go.Isosurface(\n", " x=X.flatten(),\n", " y=Y.flatten(),\n", " z=Z.flatten(),\n", " value=f(X, Y, Z).flatten(),\n", " isomin=-100,\n", " isomax=100,\n", " surface=dict(show=True,count=1, fill=0.8),\n", " slices=go.isosurface.Slices(\n", " z=go.isosurface.slices.Z(\n", " show = True,\n", " locations=[-0.3, 0.5])\n", " ),\n", " caps=go.isosurface.Caps(\n", " z=dict(show=False),\n", " x=dict(show=False),\n", " y=dict(show=False)\n", " ),\n", ")]\n", "\n", "layout = go.Layout(\n", " margin=dict(t=0, l=0, b=0),\n", " scene=dict(\n", " camera=dict(\n", " eye=dict(\n", " x=1.86,\n", " y=0.61,\n", " z=0.98\n", " )\n", " )\n", " )\n", ")\n", "\n", "fig = go.Figure(data, layout)\n", "\n", "py.iplot(fig, config=dict(showSendToCloud=True), filename='isosurface-with-slices')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Multiple Isosurfaces with Caps" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "import plotly.io as pio\n", "\n", "import numpy as np\n", "\n", "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) +\\\n", " 54*(x*y*z) + 126*(x*y + x*z + y*z) - 9*(x**2 + y**2 + z**2) - 9*(x + y + z) + 1\n", "\n", "a = 1\n", "X, Y, Z = np.mgrid[-a:a:25j, -a:a:25j, -a:a:25j]\n", "\n", "data = [go.Isosurface(\n", " x=X.flatten(),\n", " y=Y.flatten(),\n", " z=Z.flatten(),\n", " value=f(X, Y, Z).flatten(),\n", " isomin=-10,\n", " isomax=10,\n", " surface=dict(show=True,count=4, fill=0.8, pattern='odd'),\n", " caps=go.isosurface.Caps(\n", " z=dict(show=True),\n", " x=dict(show=True),\n", " y=dict(show=True)\n", " ),\n", ")]\n", "\n", "layout = go.Layout(\n", " margin=dict(t=0, l=0, b=0),\n", " template=pio.templates['plotly'],\n", " scene=dict(\n", " camera=dict(\n", " eye=dict(\n", " x=1.86,\n", " y=0.61,\n", " z=0.98\n", " )\n", " )\n", " )\n", ")\n", "\n", "fig = go.Figure(data, layout)\n", "\n", "py.iplot(fig, config=dict(showSendToCloud=True), filename='multiple-isosurface-with-caps')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/#isosurface for more information and chart attribute options!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /tmp/pip-req-build-dck2ktua\n", "Building wheels for collected packages: publisher\n", " Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n", "\u001b[?25h Stored in directory: /tmp/pip-ephem-wheel-cache-ilietpwm/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", "Successfully built publisher\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.13\n", " Uninstalling publisher-0.13:\n", " Successfully uninstalled publisher-0.13\n", "Successfully installed publisher-0.13\n", "\u001b[33mYou are using pip version 10.0.1, however version 19.0.3 is available.\n", "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", " \n", "import publisher\n", "publisher.publish(\n", " 'isosurfaces.ipynb', 'python/3d-isosurface-plots/', 'Iso Surface',\n", " 'How to make 3D Isosurface Plots in Python with Plotly.',\n", " title = 'Python 3D Isosurface Plots | plotly',\n", " name = '3D Isosurface Plots',\n", " has_thumbnail='true', thumbnail='thumbnail/isosurface.jpg', \n", " redirect_from='python/isosurfaces-with-marching-cubes/',\n", " language='python',\n", " display_as='3d_charts', order=12.1,\n", " ipynb= '~notebook_demo/272') " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "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.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }