{
"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": [
"#### Topographical 3D Surface Plot"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"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",
"import pandas as pd\n",
"\n",
"# Read data from a csv\n",
"z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')\n",
"\n",
"data = [\n",
" go.Surface(\n",
" z=z_data.as_matrix()\n",
" )\n",
"]\n",
"layout = go.Layout(\n",
" title='Mt Bruno Elevation',\n",
" autosize=False,\n",
" width=500,\n",
" height=500,\n",
" margin=dict(\n",
" l=65,\n",
" r=50,\n",
" b=65,\n",
" t=90\n",
" )\n",
")\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='elevations-3d-surface')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Surface Plot With Contours"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Display and customize contour data for each axis using the `contours` attribute ([reference](plot.ly/python/reference/#surface-contours))."
]
},
{
"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",
"\n",
"import pandas as pd\n",
"\n",
"# Read data from a csv\n",
"z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')\n",
"\n",
"data = [\n",
" go.Surface(\n",
" z=z_data.as_matrix(),\n",
" contours=go.surface.Contours(\n",
" z=go.surface.contours.Z(\n",
" show=True,\n",
" usecolormap=True,\n",
" highlightcolor=\"#42f462\",\n",
" project=dict(z=True)\n",
" )\n",
" )\n",
" )\n",
"]\n",
"layout = go.Layout(\n",
" title='Mt Bruno Elevation',\n",
" autosize=False,\n",
" scene=dict(camera=dict(eye=dict(x=1.87, y=0.88, z=-0.64))),\n",
" width=500,\n",
" height=500,\n",
" margin=dict(\n",
" l=65,\n",
" r=50,\n",
" b=65,\n",
" t=90\n",
" )\n",
")\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig, filename='elevations-3d-surface-contours')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Multiple 3D Surface Plots"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"z1 = [\n",
" [8.83,8.89,8.81,8.87,8.9,8.87],\n",
" [8.89,8.94,8.85,8.94,8.96,8.92],\n",
" [8.84,8.9,8.82,8.92,8.93,8.91],\n",
" [8.79,8.85,8.79,8.9,8.94,8.92],\n",
" [8.79,8.88,8.81,8.9,8.95,8.92],\n",
" [8.8,8.82,8.78,8.91,8.94,8.92],\n",
" [8.75,8.78,8.77,8.91,8.95,8.92],\n",
" [8.8,8.8,8.77,8.91,8.95,8.94],\n",
" [8.74,8.81,8.76,8.93,8.98,8.99],\n",
" [8.89,8.99,8.92,9.1,9.13,9.11],\n",
" [8.97,8.97,8.91,9.09,9.11,9.11],\n",
" [9.04,9.08,9.05,9.25,9.28,9.27],\n",
" [9,9.01,9,9.2,9.23,9.2],\n",
" [8.99,8.99,8.98,9.18,9.2,9.19],\n",
" [8.93,8.97,8.97,9.18,9.2,9.18]\n",
"]\n",
"\n",
"z2 = [[zij+1 for zij in zi] for zi in z1]\n",
"z3 = [[zij-1 for zij in zi] for zi in z1]\n",
"\n",
"data = [\n",
" go.Surface(z=z1),\n",
" go.Surface(z=z2, showscale=False, opacity=0.9),\n",
" go.Surface(z=z3, showscale=False, opacity=0.9)\n",
"\n",
"]\n",
"\n",
"py.iplot(data,filename='python-docs/multiple-surfaces')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dash Example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Dash](https://plotly.com/products/dash/) is an Open Source Python library which can help you convert plotly figures into a reactive, web-based application. Below is a simple example of a dashboard created using Dash. Its [source code](https://github.com/plotly/simple-example-chart-apps/tree/master/dash-3dsurfaceplot) can easily be deployed to a PaaS."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-3dsurfaceplot/\", width=\"100%\", height=950 ,frameBorder=\"0\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(src= \"https://dash-simple-apps.plotly.host/dash-3dsurfaceplot/code\", width=\"100%\", height=500 ,frameBorder=\"0\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See https://plotly.com/python/reference/#surface for more information!"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-req-build-euzlns27\n",
"Building wheels for collected packages: publisher\n",
" Building wheel for publisher (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /private/var/folders/s5/vjqn03zs7nn8zs_fwzcf14r40000gn/T/pip-ephem-wheel-cache-gsrl6w6v/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 19.0.3, however version 19.1.1 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",
" '3d-surface.ipynb', 'python/3d-surface-plots/', '3D Surface Plots in Python',\n",
" 'How to make 3D-surface plots in Python',\n",
" title= '3D Surface Plots in Python | Plotly',\n",
" has_thumbnail='true', thumbnail='thumbnail/3d-surface.jpg', \n",
" language='python', page_type='example_index', \n",
" display_as='3d_charts', order=6,\n",
" ipynb= '~notebook_demo/66')"
]
}
],
"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
}