{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING:traits.has_traits:DEPRECATED: traits.has_traits.wrapped_class, 'the 'implements' class advisor has been deprecated. Use the 'provides' class decorator.\n" ] } ], "source": [ "%matplotlib qt\n", "import numpy as np\n", "from mayavi import mlab\n", "\n", "from scipy.integrate import odeint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lorenz Attractor - 3D line and point plotting demo\n", "[Lorenz attractor](https://en.wikipedia.org/wiki/Lorenz_system) is a 3D differential equation that we will use to demonstrate mayavi's 3D plotting ability. We will look at some ways to make plotting lots of data more efficient." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# setup parameters for Lorenz equations\n", "sigma=10\n", "beta=8/3.\n", "rho=28\n", "\n", "def lorenz(x, t, ):\n", " dx = np.zeros(3)\n", " dx[0] = -sigma*x[0] + sigma*x[1]\n", " dx[1] = rho*x[0] - x[1] - x[0]*x[2]\n", " dx[2] = -beta*x[2] + x[0]*x[1]\n", " return dx" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(20000, 3)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# solve for a specific particle\n", "# initial condition\n", "y0 = np.ones(3) + .01\n", "\n", "# time steps to compute location\n", "n_time = 20000\n", "t = np.linspace(0,200,n_time)\n", "\n", "# solve the ODE \n", "y = odeint( lorenz, y0, t )\n", "\n", "y.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rendering Points and Lines\n", "Mayavi has several ways to render 3D line and point data. The default is to use surfaces, which uses more resources. There are kwargs that can be changed to make it render with 2-D lines and points that make plotting large amounts of data more efficient." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####LinePlot" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/ahaefner/anaconda/lib/python2.7/site-packages/traits/has_traits.py:1766: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.\n", " setattr( self, name, value )\n" ] } ], "source": [ "# plot the data as a line\n", "# change the tube radius to see the difference\n", "mlab.figure('Line')\n", "mlab.clf()\n", "mlab.plot3d(y[:,0], y[:,1], y[:,2], tube_radius=.1)\n", "mlab.colorbar()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# plot the data as a line, with color representing the time evolution\n", "mlab.figure('Line')\n", "mlab.clf()\n", "mlab.plot3d(y[:,0], y[:,1], y[:,2], t, tube_radius=None, )\n", "mlab.colorbar()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Point Plot" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# plot the data as a line, with color representing the time evolution\n", "mlab.figure()\n", "\n", "# By default, mayavi will plot points as spheres, so each point will \n", "# be represented by a surface. \n", "# Using mode='2dvertex' is needed for plotting large numbers of points.\n", "mlab.figure('Points')\n", "mlab.clf()\n", "mlab.points3d(y[:,0], y[:,1], y[:,2], t, mode='2dvertex')\n", "mlab.colorbar( title='time')\n", "mlab.axes()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####Line + Point Plot" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# plot the data as a line, with color representing the time evolution\n", "mlab.figure('Line and Points')\n", "mlab.clf()\n", "\n", "# plot the data as a line, with color representing the time evolution\n", "mlab.plot3d(y[:,0], y[:,1], y[:,2], t, tube_radius=None, line_width=1 )\n", "mlab.colorbar()\n", "\n", "# By default, mayavi will plot points as spheres, so each point will \n", "# be represented by a surface. \n", "# Using mode='2dvertex' is needed for plotting large numbers of points.\n", "mlab.points3d(y[:,0], y[:,1], y[:,2], t, scale_factor=.3, scale_mode='none')\n", " #mode='2dvertex')\n", "mlab.colorbar( title='time')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Contour Plot\n", "Let's see how long the particle spends in each location" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h3d = np.histogramdd(y, bins=50)\n", "\n", "# generate the midpoint coordinates\n", "xg,yg,zg = h3d[1]\n", "xm = xg[1:] - .5*(xg[1]-xg[0])\n", "ym = yg[1:] - .5*(yg[1]-yg[0])\n", "zm = zg[1:] - .5*(zg[1]-zg[0])\n", "xg, yg, zg = np.meshgrid(xm, ym, zm)\n", "\n", "mlab.figure('contour')\n", "mlab.clf()\n", "mlab.contour3d( h3d[0], opacity=.5, contours=25 )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Animation\n", "Animation can be accomplished with a mlab.animate decorator. You must define a function that yields to the animate decorator. The yield defines when mayavi will rerender the image. " ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# plot the data as a line\n", "mlab.figure('Animate')\n", "mlab.clf()\n", "# mlab.plot3d(y[:,0], y[:,1], y[:,2], tube_radius=None)\n", "# mlab.colorbar()\n", "\n", "a = mlab.points3d(y0[0], y0[1], y0[2], mode='2dvertex')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# number of points to plot\n", "# n_plot = n_time\n", "n_plot = 1000\n", "\n", "@mlab.animate(delay=10, ui=True )\n", "def anim():\n", " for i in range(n_time):\n", " # a.mlab_source.set(x=y[i,0],y=y[i,1],z=y[i,2], color=(1,0,0))\n", " mlab.points3d(y[i,0],y[i,1],y[i,2], mode='2dvertex', reset_zoom=False)\n", " yield\n", " \n", "anim()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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 }