{
"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": [
"#### Version Check\n",
"Note: Trisurfs are available in version 1.11.0+
\n",
"Run `pip install plotly --upgrade` to update your Plotly version"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2.4.1'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Torus"
]
},
{
"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.figure_factory as FF\n",
"import plotly.graph_objs as go\n",
"\n",
"import numpy as np\n",
"from scipy.spatial import Delaunay\n",
"\n",
"u = np.linspace(0, 2*np.pi, 20)\n",
"v = np.linspace(0, 2*np.pi, 20)\n",
"u,v = np.meshgrid(u,v)\n",
"u = u.flatten()\n",
"v = v.flatten()\n",
"\n",
"x = (3 + (np.cos(v)))*np.cos(u)\n",
"y = (3 + (np.cos(v)))*np.sin(u)\n",
"z = np.sin(v)\n",
"\n",
"points2D = np.vstack([u,v]).T\n",
"tri = Delaunay(points2D)\n",
"simplices = tri.simplices\n",
"\n",
"fig1 = FF.create_trisurf(x=x, y=y, z=z,\n",
" simplices=simplices,\n",
" title=\"Torus\", aspectratio=dict(x=1, y=1, z=0.3))\n",
"py.iplot(fig1, filename=\"3dFolder/Torus\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Mobius Band"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as FF\n",
"import plotly.graph_objs as go\n",
"\n",
"import numpy as np\n",
"from scipy.spatial import Delaunay\n",
"\n",
"u = np.linspace(0, 2*np.pi, 24)\n",
"v = np.linspace(-1, 1, 8)\n",
"u,v = np.meshgrid(u,v)\n",
"u = u.flatten()\n",
"v = v.flatten()\n",
"\n",
"tp = 1 + 0.5*v*np.cos(u/2.)\n",
"x = tp*np.cos(u)\n",
"y = tp*np.sin(u)\n",
"z = 0.5*v*np.sin(u/2.)\n",
"\n",
"points2D = np.vstack([u,v]).T\n",
"tri = Delaunay(points2D)\n",
"simplices = tri.simplices\n",
"\n",
"fig1 = FF.create_trisurf(x=x, y=y, z=z,\n",
" colormap=\"Portland\",\n",
" simplices=simplices,\n",
" title=\"Mobius Band\")\n",
"py.iplot(fig1, filename=\"Mobius-Band\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Boy's Surface"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import plotly.plotly as py\n",
"import plotly.figure_factory as FF\n",
"import plotly.graph_objs as go\n",
"\n",
"import numpy as np\n",
"from scipy.spatial import Delaunay\n",
"\n",
"u=np.linspace(-np.pi/2, np.pi/2, 60)\n",
"v=np.linspace(0, np.pi, 60)\n",
"u,v=np.meshgrid(u,v)\n",
"u=u.flatten()\n",
"v=v.flatten()\n",
"\n",
"x = (np.sqrt(2)*(np.cos(v)*np.cos(v))*np.cos(2*u) + np.cos(u)*np.sin(2*v))/(2 - np.sqrt(2)*np.sin(3*u)*np.sin(2*v))\n",
"y = (np.sqrt(2)*(np.cos(v)*np.cos(v))*np.sin(2*u) - np.sin(u)*np.sin(2*v))/(2 - np.sqrt(2)*np.sin(3*u)*np.sin(2*v))\n",
"z = (3*(np.cos(v)*np.cos(v)))/(2 - np.sqrt(2)*np.sin(3*u)*np.sin(2*v))\n",
"\n",
"points2D = np.vstack([u, v]).T\n",
"tri = Delaunay(points2D)\n",
"simplices = tri.simplices\n",
"\n",
"fig1 = FF.create_trisurf(x=x, y=y, z=z,\n",
" colormap=['rgb(50, 0, 75)', 'rgb(200, 0, 200)', '#c8dcc8'],\n",
" show_colorbar=True,\n",
" simplices=simplices,\n",
" title=\"Boy's Surface\")\n",
"py.iplot(fig1, filename=\"Boy's Surface\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Change Colorscale Variable"
]
},
{
"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.figure_factory as FF\n",
"import plotly.graph_objs as go\n",
"\n",
"import numpy as np\n",
"from scipy.spatial import Delaunay\n",
"\n",
"u = np.linspace(0, 2*np.pi, 20)\n",
"v = np.linspace(0, 2*np.pi, 20)\n",
"u,v = np.meshgrid(u,v)\n",
"u = u.flatten()\n",
"v = v.flatten()\n",
"\n",
"x = (3 + (np.cos(v)))*np.cos(u)\n",
"y = (3 + (np.cos(v)))*np.sin(u)\n",
"z = np.sin(v)\n",
"\n",
"points2D = np.vstack([u,v]).T\n",
"tri = Delaunay(points2D)\n",
"simplices = tri.simplices\n",
"\n",
"# define a function that calculates the distance\n",
"# from the origin to use as the color variable\n",
"def dist_origin(x, y, z):\n",
" return np.sqrt((1.0 * x)**2 + (1.0 * y)**2 + (1.0 * z)**2)\n",
"\n",
"fig1 = FF.create_trisurf(x=x, y=y, z=z, color_func=dist_origin,\n",
" colormap = [(0.4, 0.15, 0), (1, 0.65, 0.12)],\n",
" show_colorbar=True,\n",
" simplices=simplices, title=\"Torus - Origin Distance Coloring\",\n",
" aspectratio=dict(x=1, y=1, z=0.3))\n",
"py.iplot(fig1, filename=\"Torus - Origin Distance Coloring\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Diverging Colormap"
]
},
{
"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.figure_factory as FF\n",
"import plotly.graph_objs as go\n",
"\n",
"import numpy as np\n",
"from scipy.spatial import Delaunay\n",
"\n",
"u = np.linspace(-np.pi, np.pi, 30)\n",
"v = np.linspace(-np.pi, np.pi, 30)\n",
"u, v = np.meshgrid(u,v)\n",
"u = u.flatten()\n",
"v = v.flatten()\n",
"\n",
"x = u\n",
"y = u*np.cos(v)\n",
"z = u*np.sin(v)\n",
"\n",
"points2D = np.vstack([u,v]).T\n",
"tri = Delaunay(points2D)\n",
"simplices = tri.simplices\n",
"\n",
"# define a function for the color assignment\n",
"def dist_from_x_axis(x, y, z):\n",
" return x\n",
"\n",
"fig1 = FF.create_trisurf(x=x, y=y, z=z,\n",
" colormap=['rgb(255, 155, 120)', 'rgb(255, 153, 255)', ],\n",
" show_colorbar=True,\n",
" simplices=simplices, title=\"Light Cone\",\n",
" showbackground=False, gridcolor='rgb(255, 20, 160)',\n",
" plot_edges=False, aspectratio=dict(x=1, y=1, z=0.75))\n",
"py.iplot(fig1, filename=\"Light Cone\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function create_trisurf in module plotly.figure_factory._trisurf:\n",
"\n",
"create_trisurf(x, y, z, simplices, colormap=None, show_colorbar=True, scale=None, color_func=None, title='Trisurf Plot', plot_edges=True, showbackground=True, backgroundcolor='rgb(230, 230, 230)', gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', edges_color='rgb(50, 50, 50)', height=800, width=800, aspectratio=None)\n",
" Returns figure for a triangulated surface plot\n",
" \n",
" :param (array) x: data values of x in a 1D array\n",
" :param (array) y: data values of y in a 1D array\n",
" :param (array) z: data values of z in a 1D array\n",
" :param (array) simplices: an array of shape (ntri, 3) where ntri is\n",
" the number of triangles in the triangularization. Each row of the\n",
" array contains the indicies of the verticies of each triangle\n",
" :param (str|tuple|list) colormap: either a plotly scale name, an rgb\n",
" or hex color, a color tuple or a list of colors. An rgb color is\n",
" of the form 'rgb(x, y, z)' where x, y, z belong to the interval\n",
" [0, 255] and a color tuple is a tuple of the form (a, b, c) where\n",
" a, b and c belong to [0, 1]. If colormap is a list, it must\n",
" contain the valid color types aforementioned as its members\n",
" :param (bool) show_colorbar: determines if colorbar is visible\n",
" :param (list|array) scale: sets the scale values to be used if a non-\n",
" linearly interpolated colormap is desired. If left as None, a\n",
" linear interpolation between the colors will be excecuted\n",
" :param (function|list) color_func: The parameter that determines the\n",
" coloring of the surface. Takes either a function with 3 arguments\n",
" x, y, z or a list/array of color values the same length as\n",
" simplices. If None, coloring will only depend on the z axis\n",
" :param (str) title: title of the plot\n",
" :param (bool) plot_edges: determines if the triangles on the trisurf\n",
" are visible\n",
" :param (bool) showbackground: makes background in plot visible\n",
" :param (str) backgroundcolor: color of background. Takes a string of\n",
" the form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive\n",
" :param (str) gridcolor: color of the gridlines besides the axes. Takes\n",
" a string of the form 'rgb(x,y,z)' x,y,z are between 0 and 255\n",
" inclusive\n",
" :param (str) zerolinecolor: color of the axes. Takes a string of the\n",
" form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive\n",
" :param (str) edges_color: color of the edges, if plot_edges is True\n",
" :param (int|float) height: the height of the plot (in pixels)\n",
" :param (int|float) width: the width of the plot (in pixels)\n",
" :param (dict) aspectratio: a dictionary of the aspect ratio values for\n",
" the x, y and z axes. 'x', 'y' and 'z' take (int|float) values\n",
" \n",
" Example 1: Sphere\n",
" ```\n",
" # Necessary Imports for Trisurf\n",
" import numpy as np\n",
" from scipy.spatial import Delaunay\n",
" \n",
" import plotly.plotly as py\n",
" from plotly.figure_factory import create_trisurf\n",
" from plotly.graph_objs import graph_objs\n",
" \n",
" # Make data for plot\n",
" u = np.linspace(0, 2*np.pi, 20)\n",
" v = np.linspace(0, np.pi, 20)\n",
" u,v = np.meshgrid(u,v)\n",
" u = u.flatten()\n",
" v = v.flatten()\n",
" \n",
" x = np.sin(v)*np.cos(u)\n",
" y = np.sin(v)*np.sin(u)\n",
" z = np.cos(v)\n",
" \n",
" points2D = np.vstack([u,v]).T\n",
" tri = Delaunay(points2D)\n",
" simplices = tri.simplices\n",
" \n",
" # Create a figure\n",
" fig1 = create_trisurf(x=x, y=y, z=z, colormap=\"Rainbow\",\n",
" simplices=simplices)\n",
" # Plot the data\n",
" py.iplot(fig1, filename='trisurf-plot-sphere')\n",
" ```\n",
" \n",
" Example 2: Torus\n",
" ```\n",
" # Necessary Imports for Trisurf\n",
" import numpy as np\n",
" from scipy.spatial import Delaunay\n",
" \n",
" import plotly.plotly as py\n",
" from plotly.figure_factory import create_trisurf\n",
" from plotly.graph_objs import graph_objs\n",
" \n",
" # Make data for plot\n",
" u = np.linspace(0, 2*np.pi, 20)\n",
" v = np.linspace(0, 2*np.pi, 20)\n",
" u,v = np.meshgrid(u,v)\n",
" u = u.flatten()\n",
" v = v.flatten()\n",
" \n",
" x = (3 + (np.cos(v)))*np.cos(u)\n",
" y = (3 + (np.cos(v)))*np.sin(u)\n",
" z = np.sin(v)\n",
" \n",
" points2D = np.vstack([u,v]).T\n",
" tri = Delaunay(points2D)\n",
" simplices = tri.simplices\n",
" \n",
" # Create a figure\n",
" fig1 = create_trisurf(x=x, y=y, z=z, colormap=\"Viridis\",\n",
" simplices=simplices)\n",
" # Plot the data\n",
" py.iplot(fig1, filename='trisurf-plot-torus')\n",
" ```\n",
" \n",
" Example 3: Mobius Band\n",
" ```\n",
" # Necessary Imports for Trisurf\n",
" import numpy as np\n",
" from scipy.spatial import Delaunay\n",
" \n",
" import plotly.plotly as py\n",
" from plotly.figure_factory import create_trisurf\n",
" from plotly.graph_objs import graph_objs\n",
" \n",
" # Make data for plot\n",
" u = np.linspace(0, 2*np.pi, 24)\n",
" v = np.linspace(-1, 1, 8)\n",
" u,v = np.meshgrid(u,v)\n",
" u = u.flatten()\n",
" v = v.flatten()\n",
" \n",
" tp = 1 + 0.5*v*np.cos(u/2.)\n",
" x = tp*np.cos(u)\n",
" y = tp*np.sin(u)\n",
" z = 0.5*v*np.sin(u/2.)\n",
" \n",
" points2D = np.vstack([u,v]).T\n",
" tri = Delaunay(points2D)\n",
" simplices = tri.simplices\n",
" \n",
" # Create a figure\n",
" fig1 = create_trisurf(x=x, y=y, z=z, colormap=[(0.2, 0.4, 0.6), (1, 1, 1)],\n",
" simplices=simplices)\n",
" # Plot the data\n",
" py.iplot(fig1, filename='trisurf-plot-mobius-band')\n",
" ```\n",
" \n",
" Example 4: Using a Custom Colormap Function with Light Cone\n",
" ```\n",
" # Necessary Imports for Trisurf\n",
" import numpy as np\n",
" from scipy.spatial import Delaunay\n",
" \n",
" import plotly.plotly as py\n",
" from plotly.figure_factory import create_trisurf\n",
" from plotly.graph_objs import graph_objs\n",
" \n",
" # Make data for plot\n",
" u=np.linspace(-np.pi, np.pi, 30)\n",
" v=np.linspace(-np.pi, np.pi, 30)\n",
" u,v=np.meshgrid(u,v)\n",
" u=u.flatten()\n",
" v=v.flatten()\n",
" \n",
" x = u\n",
" y = u*np.cos(v)\n",
" z = u*np.sin(v)\n",
" \n",
" points2D = np.vstack([u,v]).T\n",
" tri = Delaunay(points2D)\n",
" simplices = tri.simplices\n",
" \n",
" # Define distance function\n",
" def dist_origin(x, y, z):\n",
" return np.sqrt((1.0 * x)**2 + (1.0 * y)**2 + (1.0 * z)**2)\n",
" \n",
" # Create a figure\n",
" fig1 = create_trisurf(x=x, y=y, z=z,\n",
" colormap=['#FFFFFF', '#E4FFFE',\n",
" '#A4F6F9', '#FF99FE',\n",
" '#BA52ED'],\n",
" scale=[0, 0.6, 0.71, 0.89, 1],\n",
" simplices=simplices,\n",
" color_func=dist_origin)\n",
" # Plot the data\n",
" py.iplot(fig1, filename='trisurf-plot-custom-coloring')\n",
" ```\n",
" \n",
" Example 5: Enter color_func as a list of colors\n",
" ```\n",
" # Necessary Imports for Trisurf\n",
" import numpy as np\n",
" from scipy.spatial import Delaunay\n",
" import random\n",
" \n",
" import plotly.plotly as py\n",
" from plotly.figure_factory import create_trisurf\n",
" from plotly.graph_objs import graph_objs\n",
" \n",
" # Make data for plot\n",
" u=np.linspace(-np.pi, np.pi, 30)\n",
" v=np.linspace(-np.pi, np.pi, 30)\n",
" u,v=np.meshgrid(u,v)\n",
" u=u.flatten()\n",
" v=v.flatten()\n",
" \n",
" x = u\n",
" y = u*np.cos(v)\n",
" z = u*np.sin(v)\n",
" \n",
" points2D = np.vstack([u,v]).T\n",
" tri = Delaunay(points2D)\n",
" simplices = tri.simplices\n",
" \n",
" \n",
" colors = []\n",
" color_choices = ['rgb(0, 0, 0)', '#6c4774', '#d6c7dd']\n",
" \n",
" for index in range(len(simplices)):\n",
" colors.append(random.choice(color_choices))\n",
" \n",
" fig = create_trisurf(\n",
" x, y, z, simplices,\n",
" color_func=colors,\n",
" show_colorbar=True,\n",
" edges_color='rgb(2, 85, 180)',\n",
" title=' Modern Art'\n",
" )\n",
" \n",
" py.iplot(fig, filename=\"trisurf-plot-modern-art\")\n",
" ```\n",
"\n"
]
}
],
"source": [
"help(FF.create_trisurf)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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 c:\\users\\brand\\appdata\\local\\temp\\pip-req-build-54txs94e\n",
"Installing collected packages: publisher\n",
" Found existing installation: publisher 0.11\n",
" Uninstalling publisher-0.11:\n",
" Successfully uninstalled publisher-0.11\n",
" Running setup.py install for publisher: started\n",
" Running setup.py install for publisher: finished with status 'done'\n",
"Successfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"You are using pip version 10.0.0, however version 10.0.1 is available.\n",
"You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n",
"C:\\Python27\\lib\\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",
"C:\\Python27\\lib\\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",
" \n",
"import publisher\n",
"publisher.publish(\n",
" 'trisurf.ipynb', 'python/trisurf/', 'Trisurf Plots',\n",
" 'How to make tri-surf plots in Python with Plotly. Trisurfs are formed by replacing the boundaries of a compact surface by touching triangles.',\n",
" title = 'Python Trisurf Plots | plotly',\n",
" name = 'Trisurf Plots',\n",
" has_thumbnail='true', thumbnail='thumbnail/tri-surf2.jpg', \n",
" language='python',\n",
" display_as='3d_charts', order=10,\n",
" ipynb= '~notebook_demo/70') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"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.14"
}
},
"nbformat": 4,
"nbformat_minor": 1
}