{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Strips on sphere ##" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We generate a sphere from 12 strips. The basic strip is parameterized and discretized below:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "theta=np.linspace(0, 5*np.pi/36, 30) #5pi/36=25 degrees\n", "phi=np.linspace(-np.pi/2, np.pi/2, 100)\n", "theta, phi=np.meshgrid(theta, phi)\n", "x=np.cos(theta)*np.cos(phi)\n", "y=np.sin(theta)*np.cos(phi)\n", "z=np.sin(phi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This strip is rotated about Oz, with integer multiple of $\\pi/6$ radians. Since z-rotation does not change z values, we define only a planar rotation of `alpha` radians:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def Rot(alpha):\n", " return np.array([[np.cos(alpha), -np.sin(alpha)], [np.sin(alpha), np.cos(alpha)]])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of applying this rotation successively to each point in the basic strip, we make use of the numpy function `einsum` (Einstein summation), and rotate simultaneously all points with coordinates in two equally shaped meshgrids (x,y, in our case): " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def rotate_z_mesh(x, y, alpha):\n", " return np.einsum('ji, mni -> jmn', Rot(alpha), np.dstack([x, y]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The colorscale to plot the sphere's strips:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cofee_green=[[0.0,u'#543005'],\n", "[0.1,u'#8c510a'],\n", "[0.2, u'#bf812d'],\n", "[0.3, u'#dfc27d'],\n", "[0.4, u'#f6e8c3'],\n", "[0.5, u'#f5f5f5'],\n", "[0.6, u'#c7eae5'],\n", "[0.7, u'#80cdc1'],\n", "[0.8, u'#35978f'],\n", "[0.9, u'#01665e'],\n", "[1.0, u'#003c30']]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "import plotly.tools as tls\n", "from plotly.graph_objs import *" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def make_trace(x,y,z, colsc):# this function creates the trace for each strip on sphere\n", " return Surface(\n", " x=x,\n", " y=y,\n", " z=z,\n", " colorscale=colsc, \n", " name='' \n", " )" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dt=[make_trace(x,y,z, cofee_green)]#initial strip\n", "for k in range(1,12):\n", " rez=rotate_z_mesh(x, y, k*np.pi/6)\n", " dt+=[make_trace(rez[0], rez[1], z, cofee_green)]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data=Data(dt)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The draw time for this plot will be slow for clients without much RAM.\n" ] }, { "data": { "text/plain": [ "u'https://plot.ly/~empet/4759'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "axis = dict(\n", "showbackground=True, \n", "backgroundcolor='rgb(20, 20, 20)', \n", "gridcolor='rgb(75, 75, 75)', \n", "zerolinecolor='rgb(75, 75, 75)'\n", " )\n", "\n", "layout = Layout(\n", " title='Strips on sphere', \n", " width=800,\n", " height=800,\n", " scene=Scene( \n", " xaxis=XAxis(axis),\n", " yaxis=YAxis(axis), \n", " zaxis=ZAxis(axis), \n", " )\n", " )\n", " \n", "fig = Figure(data=data, layout=layout)\n", "py.sign_in('empet', 'my_api_key')\n", "py.plot(fig, filename='spherical-strips')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[https://plot.ly/~empet/4759](https://plot.ly/~empet/4759)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 31, "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.9" } }, "nbformat": 4, "nbformat_minor": 0 }