{ "metadata": { "name": "", "signature": "sha256:3b25fea37a3c46e73cc132eda67fab01355bde6c584bbbd8bf2c4f5aeab06812" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "More 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.19'" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If not version 1.0.19 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. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's try to make a Plotly version of this plot taken from Roberto Colistete's web page.
Big thanks!\n", "\n", "The function to be plotted is:\n", "\n", "$$ f(x,y) = A \\cos(\\pi x y) e^{-(x^2+y^2)/2} $$\n", "\n", "where $A$ is some number corresponding to the amplitude of the surface. So," ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", " \n", "def fxy(A, L):\n", " \"\"\"\n", " A is the amplitude of the curve\n", " L is the length of a side of the square\n", " \"\"\"\n", " \n", " x = np.arange(-L/2., L/2., 0.1, float)\n", " y = x[:,np.newaxis]\n", " \n", " return A*(np.cos(np.pi*x*y))**2*np.exp(-(x**2+y**2)/2.)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "A = 10 # choose a maximum amplitude\n", "L = 4 # choose length of square domain\n", "\n", "# Get coordinate arrays\n", "z = fxy(A,L)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# Print shape of z\n", "z.shape " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "(40, 40)" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "z.min(), z.max()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "(2.0069089092572866e-32, 10.0)" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, build a trace dictionary containing the surface plot type:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_surface = dict(z=z, # z coords, a 2D array\n", " type='surface', # N.B. 'surface' plot type\n", " )" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "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": 8, "text": [ "{'data': [{'type': 'surface',\n", " 'z': array([[ 0.18315639, 0.14568834, 0.02557518, ..., 0.03046634,\n", " 0.02557518, 0.14568834],\n", " [ 0.14568834, 0.0310402 , 0.02013064, ..., 0.21816952,\n", " 0.02013064, 0.0310402 ],\n", " [ 0.02557518, 0.02013064, 0.20811508, ..., 0.45015744,\n", " 0.20811508, 0.02013064],\n", " ..., \n", " [ 0.03046634, 0.21816952, 0.45015744, ..., 0.4919921 ,\n", " 0.45015744, 0.21816952],\n", " [ 0.02557518, 0.02013064, 0.20811508, ..., 0.45015744,\n", " 0.20811508, 0.02013064],\n", " [ 0.14568834, 0.0310402 , 0.02013064, ..., 0.21816952,\n", " 0.02013064, 0.0310402 ]])}]}" ] } ], "prompt_number": 8 }, { "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-fxy')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "u'https://plot.ly/~etpinard/265'" ] } ], "prompt_number": 9 }, { "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-fxy')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "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": 11, "text": [ "" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 } ], "metadata": {} } ] }