{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### New to Plotly?\n",
"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/).\n",
" 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).\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": [
"#### Projections of a 3d surface onto planes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This example will demonstrate how to create heatmaps of projections of a 3d surface onto planes perpendicular to the z, x, respectively y-direction. Usually surfaces in the 3d space are colored with a colormap associated to the normalized range of the z coordinates of points on that surface.\n",
"Recently, Plotly devised a method to color a surface according to a custom color function.\n",
"\n",
"Namely, if $x, y, z$ are numpy arrays of shape (m, n), defined by a discretization (via a meshgrid) of a surface z=f(x,y) or in parametric form, $x=x(u,v), y=y(u,v), z=z(u,v)$, then a custom function, `Color(x,y,z)`, returns a numpy array `C`, of the same shape as z, and the surface is colored by a colormap, according to the values in `C`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This method allows to project a surface onto planes perpendicular to the z, x or y-direction in the 3d space\n",
"and interpret the projection as a planar surface colored according to the z, x or y value at each point\n",
"of the coresponding plane.\n",
"\n",
"First, define the surface and its discretization:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"import numpy as np\n",
"\n",
"xx=np.linspace(-3.5, 3.5, 100)\n",
"yy=np.linspace(-3.5, 3.5, 100)\n",
"x,y=np.meshgrid(xx, yy)\n",
"z=np.exp(-(x-1)**2-y**2)-10*(x**3+y**4-x/5)*np.exp(-(x**2+y**2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Color according to normalized z-values"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"colorscale=[[0.0, 'rgb(20,29,67)'],\n",
" [0.1, 'rgb(28,76,96)'],\n",
" [0.2, 'rgb(16,125,121)'],\n",
" [0.3, 'rgb(92,166,133)'],\n",
" [0.4, 'rgb(182,202,175)'],\n",
" [0.5, 'rgb(253,245,243)'],\n",
" [0.6, 'rgb(230,183,162)'],\n",
" [0.7, 'rgb(211,118,105)'],\n",
" [0.8, 'rgb(174,63,95)'],\n",
" [0.9, 'rgb(116,25,93)'],\n",
" [1.0, 'rgb(51,13,53)']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Add hover text for the surface:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"textz = [['x: '+'{:0.5f}'.format(x[i][j])+' y: '+'{:0.5f}'.format(y[i][j])+\n",
" ' z: '+'{:0.5f}'.format(z[i][j]) for j in range(z.shape[1])] for i in range(z.shape[0])]\n",
"\n",
"trace1= go.Surface(\n",
" x=tuple(x),\n",
" y=tuple(y),\n",
" z=tuple(z),\n",
" colorscale=colorscale,\n",
" text=textz,\n",
" hoverinfo='text',\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Set Plot Layout:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"axis = dict(\n",
"showbackground=True, \n",
"backgroundcolor=\"rgb(230, 230,230)\", \n",
"showgrid=False, \n",
"zeroline=False, \n",
"showline=False)\n",
"\n",
"ztickvals=list(range(-6,4))\n",
"layout = go.Layout(title=\"Projections of a surface onto coordinate planes\" , \n",
" autosize=False,\n",
" width=700,\n",
" height=600,\n",
" scene=dict(xaxis=dict(axis, range=[-3.5, 3.5]),\n",
" yaxis=dict(axis, range=[-3.5, 3.5]),\n",
" zaxis=dict(axis , tickvals=ztickvals),\n",
" aspectratio=dict(x=1,\n",
" y=1,\n",
" z=0.95)\n",
" )\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Discretization of each Plane\n",
"The surface projections will be plotted in the planes of equations\n",
"`Z=np.min(z)-2`, `X=np.min(xx)`, respectively `Y=np.min(yy)`."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"z_offset=(np.min(z)-2)*np.ones(z.shape)#\n",
"x_offset=np.min(xx)*np.ones(z.shape)\n",
"y_offset=np.min(yy)*np.ones(z.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the color functions and the color numpy arrays, `C_z`, `C_x`, `C_y`, corresponding to each plane: \n",
"Define the 3-tuples of coordinates to be displayed at hovering the mouse over the projections. \n",
"The first two coordinates give the position in the projection plane, whereas the third one is used \n",
"for assigning the color, just in the same way the coordinate z is used for the z-direction projection."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"proj_z=lambda x, y, z: z#projection in the z-direction\n",
"colorsurfz=proj_z(x,y,z)\n",
"proj_x=lambda x, y, z: x\n",
"colorsurfx=proj_z(x,y,z)\n",
"proj_y=lambda x, y, z: y\n",
"colorsurfy=proj_z(x,y,z)\n",
"\n",
"textx=[['y: '+'{:0.5f}'.format(y[i][j])+' z: '+'{:0.5f}'.format(z[i][j])+\n",
" ' x: '+'{:0.5f}'.format(x[i][j]) for j in range(z.shape[1])] for i in range(z.shape[0])]\n",
"texty=[['x: '+'{:0.5f}'.format(x[i][j])+' z: '+'{:0.5f}'.format(z[i][j]) +\n",
" ' y: '+'{:0.5f}'.format(y[i][j]) for j in range(z.shape[1])] for i in range(z.shape[0])] \n",
"\n",
"tracex = go.Surface(z=list(z),\n",
" x=list(x_offset),\n",
" y=list(y),\n",
" colorscale=colorscale,\n",
" showlegend=False,\n",
" showscale=False,\n",
" surfacecolor=colorsurfx,\n",
" text=textx,\n",
" hoverinfo='text'\n",
" )\n",
"tracey = go.Surface(z=list(z),\n",
" x=list(x),\n",
" y=list(y_offset),\n",
" colorscale=colorscale,\n",
" showlegend=False,\n",
" showscale=False,\n",
" surfacecolor=colorsurfy,\n",
" text=texty,\n",
" hoverinfo='text'\n",
" )\n",
"tracez = go.Surface(z=list(z_offset),\n",
" x=list(x),\n",
" y=list(y),\n",
" colorscale=colorscale,\n",
" showlegend=False,\n",
" showscale=False,\n",
" surfacecolor=colorsurfx,\n",
" text=textz,\n",
" hoverinfo='text'\n",
" )\n",
"\n",
"data=[trace1, tracex, tracey, tracez]\n",
"fig = go.Figure(data=data, layout=layout)\n",
"py.iplot(fig)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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-AxIM_I\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-5sJcIZ/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"
]
}
],
"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",
" 'Plotly-project-3d-onto-a-plane.ipynb', 'python/2d-projection-of-3d-surface/', 'Projection of 3D Surface',\n",
" 'How to project 3D Surface plots in 2D with Plotly.',\n",
" title = '2D Projection of 3D surface | plotly',\n",
" has_thumbnail='true', thumbnail='thumbnail/projection-3d.jpg', \n",
" language='python',\n",
" display_as='3d_charts', order=19,\n",
" ipynb= '~notebook_demo/79')"
]
},
{
"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
}