{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Subplots 3D ##" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import scipy.integrate as integrate\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subplot 1, Chua's attractor" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def Chua((x, y, z), t0, k=1, alpha=15.6, beta=28, gamma=0, m0=-1.143, m1=-0.714):\n", " h=lambda x: m1*x+0.5*(m0-m1)*(abs(x+1)-abs(x-1))\n", " \n", " return [k*alpha*(y-x-h(x)), k*(x - y+ z), -k*(beta*y+gamma*z)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Integrate Chua's system of differential equations defined by the function `Chua`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x0=[0.7, 0,0]\n", "t = np.linspace(0, 100, 2000)\n", "xt = integrate.odeint(Chua, x0, t)# xt is the solution x(t) as a numpy arrayof shape (2000, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subplot 2, Hamiltonian surface $z=H(x,y)$:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "H=lambda x,y: 0.5*y**2+x**3/3.0-x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Surface discretization: " ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x2=np.arange(-2, 2, 0.1)\n", "y2=np.arange(-2, 2, 0.1) \n", "X2,Y2=np.meshgrid(x2,y2)\n", "z2=H(X2,Y2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subplot 3, Mount Bruno:\n", " " ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "dem6cs=[[0.0, '#32924c'], # custom colorscale\n", " [0.1, '#52a157'],\n", " [0.2, '#74b162'],\n", " [0.3, '#94c06d'],\n", " [0.4, '#b6d079'],\n", " [0.5, '#d7de84'],\n", " [0.6, '#c9c370'],\n", " [0.7, '#bba65b'],\n", " [0.8, '#ad8a47'],\n", " [0.9, '#9f6d32'],\n", " [1.0, '#91511e']]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#Data source: https://plot.ly/~mariahh/143/\n", "z3=np.loadtxt('Data/Mount-Bruno.txt')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subplot 4, Dini surface:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "u=np.linspace(0, 4*np.pi, 300)\n", "v=np.linspace(0.1, 1.2, 100)\n", "u,v=np.meshgrid(u,v)\n", "x4=np.cos(u)*np.sin(v)\n", "y4=np.sin(u)*np.sin(v)\n", "z4=np.cos(v)+np.log(np.tan(v/2))+0.2*u" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "from plotly.graph_objs import *\n", "from plotly import tools\n", "py.sign_in('empet', 'my_api_key')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the format of your plot grid:\n", "[ (1,1) scene1 ] [ (1,2) scene2 ]\n", "[ (2,1) scene3 ] [ (2,2) scene4 ]\n", "\n" ] } ], "source": [ "fig = tools.make_subplots(rows=2, cols=2, specs=[ [{'is_3d': True}, {'is_3d': True}],\n", " [{'is_3d': True}, {'is_3d': True}] ] )\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Having comma after z4, x4 and y4 they are interpreted as tuples of shape (1,).\n", "That is why we take in the trace definition x4[0], etc." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig.append_trace(dict(type='scatter3d', x=xt[:,0], y=xt[:,1], z=xt[:,2], mode='lines',\n", " scene='scene1', showlegen=False), 1, 1)\n", "fig.append_trace(dict(type='surface', x=x2, y=y2, z=z2, colorscale='Viridis',\n", " scene='scene2', showscale=False), 1, 2)\n", "fig.append_trace(dict(type='surface', z=z3, colorscale=dem6cs,\n", " scene='scene3', showscale=False), 2, 1)\n", "fig.append_trace(dict(type='surface', x=x4, y=y4, z=z4, colorscale='Greens',\n", " scene='scene4', showscale=False), 2, 2)\n", "\n", "fig['layout'].update(title=\"Subplots 3D.
1. Chua's attractor, 2. Hamiltonian (surface)\"+\\\n", " \"3. Mount Bruno, 4. Dini Surface\",\n", " height=800, width=800, showlegend=False)\n", "\n", "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", "scene=Scene( \n", " xaxis=XAxis(axis),\n", " yaxis=YAxis(axis), \n", " zaxis=ZAxis(axis)\n", " )\n", "fig['layout']['scene1'].update(scene)\n", "fig['layout']['scene2'].update(scene)\n", "fig['layout']['scene3'].update(scene)\n", "fig['layout']['scene4'].update(scene)\n", " \n", " \n", "py.iplot(fig, filename='Subplots-3D', validate=False)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 15, "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 }