{
"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!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Version Check\n",
"Note: 3D Mesh are available in version 1.10.0+
\n",
"Run `pip install plotly --upgrade` to update your Plotly version"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2.0.15'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Simple 3D Mesh example ###"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are using data present in a separate text file. The file can be downloaded from [Plotly's dataset repo](https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt)."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"import numpy as np\n",
"\n",
"pts=np.loadtxt('mesh_dataset.txt')\n",
"x,y,z=zip(*pts)\n",
"\n",
"trace = go.Mesh3d(x=x,y=y,z=z,color='#FFB6C1',opacity=0.50)\n",
"py.iplot([trace])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3D Mesh example with Alphahull"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alphahull sets shape of mesh. If the value is -1 then Delaunay triangulation is used. If >0 then the alpha-shape algorithm is used. The default value is -1."
]
},
{
"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",
"import numpy as np\n",
"\n",
"pts=np.loadtxt('mesh_dataset.txt')\n",
"x,y,z=zip(*pts)\n",
"\n",
"trace = go.Mesh3d(x=x,y=y,z=z,\n",
" alphahull=5,\n",
" opacity=0.4,\n",
" color='#00FFFF')\n",
"py.iplot([trace])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mesh Tetrahedron"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = [\n",
" go.Mesh3d(\n",
" x = [0, 1, 2, 0],\n",
" y = [0, 0, 1, 2],\n",
" z = [0, 2, 0, 1],\n",
" colorbar = go.ColorBar(\n",
" title='z'\n",
" ),\n",
" colorscale = [[0, 'rgb(255, 0, 0)'], \n",
" [0.5, 'rgb(0, 255, 0)'], \n",
" [1, 'rgb(0, 0, 255)']],\n",
" intensity = [0, 0.33, 0.66, 1],\n",
" i = [0, 0, 0, 1],\n",
" j = [1, 2, 3, 2],\n",
" k = [2, 3, 1, 3],\n",
" name = 'y',\n",
" showscale = True\n",
" )\n",
"]\n",
"layout = go.Layout(\n",
" xaxis=go.XAxis(\n",
" title='x'\n",
" ),\n",
" yaxis=go.YAxis(\n",
" title='y'\n",
" )\n",
")\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='3d-mesh-tetrahedron-python')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mesh Cube"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = [\n",
" go.Mesh3d(\n",
" x = [0, 0, 1, 1, 0, 0, 1, 1],\n",
" y = [0, 1, 1, 0, 0, 1, 1, 0],\n",
" z = [0, 0, 0, 0, 1, 1, 1, 1],\n",
" colorbar = go.ColorBar(\n",
" title='z'\n",
" ),\n",
" colorscale = [[0, 'rgb(255, 0, 255)'],\n",
" [0.5, 'rgb(0, 255, 0)'], \n",
" [1, 'rgb(0, 0, 255)']],\n",
" intensity = [0, 0.142857142857143, 0.285714285714286, \n",
" 0.428571428571429, 0.571428571428571, \n",
" 0.714285714285714, 0.857142857142857, 1],\n",
" i = [7, 0, 0, 0, 4, 4, 6, 6, 4, 0, 3, 2],\n",
" j = [3, 4, 1, 2, 5, 6, 5, 2, 0, 1, 6, 3],\n",
" k = [0, 7, 2, 3, 6, 7, 1, 1, 5, 5, 7, 6],\n",
" name='y',\n",
" showscale=True\n",
" )\n",
"]\n",
"layout = go.Layout(\n",
" xaxis=go.XAxis(\n",
" title='x'\n",
" ),\n",
" yaxis=go.YAxis(\n",
" title='y'\n",
" )\n",
")\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='3d-mesh-cube-python')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reference\n",
"See https://plotly.com/python/reference/#mesh3d for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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 /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-req-build-XnpO2r\n",
"Building wheels for collected packages: publisher\n",
" Running setup.py bdist_wheel for publisher ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /private/var/folders/tc/bs9g6vrd36q74m5t8h9cgphh0000gn/T/pip-ephem-wheel-cache-VXv90N/wheels/99/3e/a0/fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n",
"Successfully built publisher\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
"Successfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning:\n",
"\n",
"The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
"\n",
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning:\n",
"\n",
"Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
"\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",
"import publisher\n",
"\n",
"publisher.publish(\n",
" 'mesh-3d.ipynb', 'python/3d-mesh/', 'Python 3D Mesh Plots | Plotly',\n",
" 'How to make 3D Mesh Plots',\n",
" title= '3D Mesh Plots in Python | plotly',\n",
" name = '3D Mesh Plots',\n",
" has_thumbnail='true', thumbnail='thumbnail/3d-mesh.jpg', \n",
" language='python',\n",
" display_as='3d_charts', order=7,\n",
" ipynb= '~notebook_demo/67') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}