{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Triangular wireplot on sphere ##" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "from __future__ import division" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sphere parameterization:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def sphere(theta, phi): \n", " x=np.cos(phi)*np.cos(theta)\n", " y=np.cos(phi)*np.sin(theta)\n", " z=np.sin(phi) \n", " return x,y,z" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "theta=np.linspace(0,2*np.pi,40)\n", "phi=np.linspace(-np.pi/2, np.pi/2, 30)\n", "theta,phi=np.meshgrid(theta,phi)\n", "x,y,z=sphere(theta,phi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose three non-collinear points in the parametric plane, i.e. give the $\\theta, \\varphi$ coordinates for these points:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "A=np.array([0, np.pi/12])\n", "B=np.array([np.pi/3, np.pi/12])\n", "C=np.array([np.pi/4, 7*np.pi/16])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "T=[A,B,C]# Parametric triangle of vertices A,B,C" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We define a triangulation of the triangle $T$ giving first the barycentric coordinates of the triangulation points, and then calculating their\n", "cartesian coordinates:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def barycentric(n):\n", " return [(i/n,j/n, 1-(i+j)/n) for i in range(n,-1, -1) for j in range(n-i, -1, -1)]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cartesian_coords=lambda T, w: w[0]*T[0]+w[1]*T[1]+w[2]*T[2]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following function selects successively three consecutive points from the list of the triangulation points, and creates a list of sub-triangles:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def triangles(n, point):\n", " triplets=[]\n", " i=0\n", " j=1\n", " for nr in range(1,n+1):\n", " for k in range(nr):\n", " triplets.append([point[i], point[j], point[j+1]])\n", " i+=1\n", " j+=1\n", " j+=1\n", " return triplets " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to plot the triangular wireframe on the sphere we are setting $n=8$, as being the number of points on each side of the triangle $T$:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n=8\n", "bar=barycentric(n)\n", "pts_tri=[cartesian_coords(T, w) for w in bar]#list of triangulation points\n", "tri=triangles(n, pts_tri)#list of sub-triangles in T" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of evaluating the parameterization only at the triangulation points, we also evaluate it at the middle of each side of a sub-triangle\n", "associated to the defined triangulation:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def divide_sides(t):\n", " pts=[t[k] for k in range(3)]+[t[0]]\n", " for k in range(1, 7, 2):\n", " m=int((k-1)/2)\n", " pts.insert(k, (t[m]+t[(m+1)%3])/2)\n", " return pts " ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "from plotly.graph_objs import *\n", "import plotly.tools as tls\n", "py.sign_in('empet', 'my_api_key')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "trace = Surface(\n", " z=z, \n", " x=x, \n", " y=y, \n", " colorscale='Viridis',\n", " )" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def line_pts(tri, surface, color='#0000A0'):\n", " # tri is the list of triangulation sub-triangles\n", " # surface is the function implementing a surface parameterization\n", " lines = []\n", " for t in tri:\n", " pts=divide_sides(t)\n", " coords=zip(*[surface(pts[k][0], pts[k][1]) for k in range(7)])\n", " lines.append(Scatter3d(x=coords[0], \n", " y=coords[1],\n", " z=coords[2], \n", " mode='lines', \n", " line=Line(color=color, \n", " width=5)))\n", " return lines " ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "u'https://plot.ly/~empet/6954'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "axis = dict(\n", "showbackground=True, \n", "backgroundcolor=\"rgb(230, 230,230)\",\n", "gridcolor=\"rgb(255, 255, 255)\", \n", "zerolinecolor=\"rgb(255, 255, 255)\", \n", " )\n", " \n", "data = Data([trace]+line_pts(tri, sphere))#plot both the sphere as a Surface object and the triangular wireframe\n", "layout = Layout(\n", " title='Triangular wireframe on sphere',\n", " width=700,\n", " height=700,\n", " scene=Scene( \n", " xaxis=XAxis(axis),\n", " yaxis=YAxis(axis), \n", " zaxis=ZAxis(axis), \n", " ),\n", " showlegend=False\n", " )\n", " \n", "\n", "fig1 = Figure(data=data, layout=layout)\n", "py.plot(fig1, filename='triangular-wireplot-2')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Now we are plotting only the triangular wireframe:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "u'https://plot.ly/~empet/6965'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data1 = Data(line_pts(tri, sphere))\n", "layout.update(width=600,\n", " height=600)\n", "fig2 = Figure(data=data1, layout=layout)\n", "py.plot(fig2, filename='triangular-wireplot-3')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In a similar way we can generate any triangular wireframe on a surface given by a parameterization or the explicit equation $z=f(x,y)$." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"./custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }