{ "metadata": { "name": "", "signature": "sha256:0f2c24022a25e609b53460a0d9ce3b7fb4ce355f58d2062d5c2f00fdc57ba56c" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "3D plots" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "with Plotly" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First check which Plotly version is installed on your machine:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly\n", "\n", "plotly.__version__" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "'1.0.17'" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If not version 1.0.17 or up, please upgrade using pip:\n", "\n", " $ pip install plotly --upgrade\n", " \n", "or\n", "\n", " $ sudo pip install plotly --upgrade" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, import the `plotly` and `tools` module and sign in using your credentials file: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly.plotly as py # (New syntax!) tools to communicate with Plotly's server\n", "import plotly.tools as tls # (NEW!) useful Python/Plotly tools \n", "\n", "my_creds = tls.get_credentials_file() # read credentials\n", "py.sign_in(my_creds['username'], my_creds['api_key']) # (New syntax!) Plotly sign in" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "> Graph objects for 3d plots have yet to be inserted in the latest version of the Plotly package; hence, we will use standard Python dictionaries and list to create our figure in this notebook. \n", "\n", "The following example is just snippet of things to come." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "1. A 3D Gaussian surface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's start by making a simple 3D Gaussian surface (the below function was inspired by this stackoverflow answer, thanks)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", " \n", "def makeGaussian(A, L, fwhm = 3, center=None):\n", " \"\"\" Make a square gaussian kernel.\n", " \n", " A is the amplitude of the curve\n", " L is the length of a side of the square\n", " fwhm is full-width-half-maximum, which\n", " can be thought of as an effective radius.\n", " \"\"\"\n", " \n", " x = np.arange(0, L, 0.1, float)\n", " y = x[:,np.newaxis]\n", " \n", " if center is None:\n", " x0 = y0 = L // 2\n", " else:\n", " x0 = center[0]\n", " y0 = center[1]\n", " \n", " return (x, y, A*np.exp(-4*np.log(2) * ((x-x0)**2 + (y-y0)**2) / fwhm**2))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "A = 10 # choose a maximum amplitude\n", "L = 8 # choose length of square domain\n", "\n", "# Get coordinate arrays\n", "x,y,z = makeGaussian(A,L)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# Print shape of x,y and z\n", "x.shape, y.shape, z.shape " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "((80,), (80, 1), (80, 80))" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, build a trace dictionary containing the surface plot type:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_surface = dict(x=x, # x coords (optional)\n", " y=y, # y coords (optional)\n", " z=z, # z coords, a 2D array\n", " type='surface', # N.B. 'surface' plot type\n", " )" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a correponding figure dictionary:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_fig = dict(data=[my_surface]) # N.B. value link to 'data' must be a list\n", "\n", "my_fig # print figure dictionary below" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "{'data': [{'type': 'surface',\n", " 'x': array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ,\n", " 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1,\n", " 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2,\n", " 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3,\n", " 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4,\n", " 5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5,\n", " 6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6,\n", " 7.7, 7.8, 7.9]),\n", " 'y': array([[ 0. ],\n", " [ 0.1],\n", " [ 0.2],\n", " [ 0.3],\n", " [ 0.4],\n", " [ 0.5],\n", " [ 0.6],\n", " [ 0.7],\n", " [ 0.8],\n", " [ 0.9],\n", " [ 1. ],\n", " [ 1.1],\n", " [ 1.2],\n", " [ 1.3],\n", " [ 1.4],\n", " [ 1.5],\n", " [ 1.6],\n", " [ 1.7],\n", " [ 1.8],\n", " [ 1.9],\n", " [ 2. ],\n", " [ 2.1],\n", " [ 2.2],\n", " [ 2.3],\n", " [ 2.4],\n", " [ 2.5],\n", " [ 2.6],\n", " [ 2.7],\n", " [ 2.8],\n", " [ 2.9],\n", " [ 3. ],\n", " [ 3.1],\n", " [ 3.2],\n", " [ 3.3],\n", " [ 3.4],\n", " [ 3.5],\n", " [ 3.6],\n", " [ 3.7],\n", " [ 3.8],\n", " [ 3.9],\n", " [ 4. ],\n", " [ 4.1],\n", " [ 4.2],\n", " [ 4.3],\n", " [ 4.4],\n", " [ 4.5],\n", " [ 4.6],\n", " [ 4.7],\n", " [ 4.8],\n", " [ 4.9],\n", " [ 5. ],\n", " [ 5.1],\n", " [ 5.2],\n", " [ 5.3],\n", " [ 5.4],\n", " [ 5.5],\n", " [ 5.6],\n", " [ 5.7],\n", " [ 5.8],\n", " [ 5.9],\n", " [ 6. ],\n", " [ 6.1],\n", " [ 6.2],\n", " [ 6.3],\n", " [ 6.4],\n", " [ 6.5],\n", " [ 6.6],\n", " [ 6.7],\n", " [ 6.8],\n", " [ 6.9],\n", " [ 7. ],\n", " [ 7.1],\n", " [ 7.2],\n", " [ 7.3],\n", " [ 7.4],\n", " [ 7.5],\n", " [ 7.6],\n", " [ 7.7],\n", " [ 7.8],\n", " [ 7.9]]),\n", " 'z': array([[ 0.00052322, 0.00066739, 0.00084606, ..., 0.00106596,\n", " 0.00084606, 0.00066739],\n", " [ 0.00066739, 0.00085128, 0.00107918, ..., 0.00135968,\n", " 0.00107918, 0.00085128],\n", " [ 0.00084606, 0.00107918, 0.00136808, ..., 0.00172368,\n", " 0.00136808, 0.00107918],\n", " ..., \n", " [ 0.00106596, 0.00135968, 0.00172368, ..., 0.0021717 ,\n", " 0.00172368, 0.00135968],\n", " [ 0.00084606, 0.00107918, 0.00136808, ..., 0.00172368,\n", " 0.00136808, 0.00107918],\n", " [ 0.00066739, 0.00085128, 0.00107918, ..., 0.00135968,\n", " 0.00107918, 0.00085128]])}]}" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are now ready to send the figure dictionary (or figure object) to Plotly. As graph objects for 3D plots are missing for the current release of the Plotly package, we must turn off the automatic key-value validation by adding the `validate=False` keyword argument in the `py.plot()` call. Otherwise, we would get an error and no plot.\n", "\n", "So," ] }, { "cell_type": "code", "collapsed": false, "input": [ "py.plot(my_fig, validate=False, filename='test-3d')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "u'https://plot.ly/~etpinard/234'" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Where the above is the unique URL corresponding to our Plotly plot.\n", "\n", "Or, inside an IPython notebook, use:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "py.iplot(my_fig, validate=False, filename='test-3d')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "*And there you go.* An interactive 3D plot from Plotly.\n", "\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "
\n", "\n", "

Got Questions or Feedback?

\n", "\n", "About Plotly\n", "\n", "* email: feedback@plot.ly \n", "* tweet: \n", "@plotlygraphs\n", "\n", "

Notebook styling ideas

\n", "\n", "Big thanks to\n", "\n", "* Cam Davidson-Pilon\n", "* Lorena A. Barba\n", "\n", "
" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# CSS styling within IPython notebook\n", "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"../../python-user-guide/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 } ], "metadata": {} } ] }