Professional Documents
Culture Documents
(https://jakevdp.github.io/)
Musingsandramblingsthroughtheworldof
Pythonandbeyond
Atom(/atom.xml)
Search
Navigate
Archives(/archives.html)
HomePage(http://www.astro.washington.edu/users/vanderplas)
AnimatingtheLorenzSystemin3D
Feb16,2013
OneofthethingsIreallyenjoyaboutPythonishoweasyitmakesittosolveinterestingproblemsandvisualize
thosesolutionsinacompellingway.I'vedoneseveralpostsoncreatinganimationsusingmatplotlib'srelatively
newanimationtoolkit(http://matplotlib.sourceforge.net/api/animation_api.html):(someexamplesareachaotic
doublependulum(/blog/2012/08/18/matplotlibanimationtutorial/),thecollisionsofparticlesinabox
(/blog/2012/08/18/matplotlibanimationtutorial/),thetimeevolutionofaquantummechanicalwavefunction
(/blog/2012/09/05/quantumpython/),andevenascenefromtheclassicvideogame,SuperMarioBros.
(/blog/2013/01/13/hackingsupermariobroswithpython/)).
Recently,areadercommented(/blog/2012/08/18/matplotlibanimationtutorial/#comment799781196)asking
whetherImightdoa3Danimationexample.Matplotlibhasadecent3Dtoolkitcalledmplot3D
(http://matplotlib.org/mpl_toolkits/mplot3d/index.html),andthoughIhaven'tpreviouslyseenitusedinconjunction
withtheanimationtools,there'snothingfundamentalthatpreventsit.
Atthecommenter'ssuggestion,Idecidedtotrythisoutwithasimpleexampleofachaoticsystem:theLorenz
equations.
SolvingtheLorenzSystem
TheLorenzEquations(http://en.wikipedia.org/wiki/Lorenz_system)areasystemofthreecoupled,firstorder,
nonlineardifferentialequationswhichdescribethetrajectoryofaparticlethroughtime.Thesystemwasoriginally
derivedbyLorenzasamodelofatmosphericconvection,butthedeceptivesimplicityoftheequationshavemade
themanoftenusedexampleinfieldsbeyondatmosphericphysics.
Theequationsdescribetheevolutionofthespatialvariablesx ,y ,andz,giventhegoverningparameters , ,
and ,throughthespecificationofthetimederivativesofthespatialvariables:
dx/dt = (y x)
dy/dt = x( z) y
dz/dt = xy z
Thoughnogeneralanalyticsolutionexistsforthissystem,thesolutionscanbecomputednumerically.Python
makesthissortofproblemveryeasytosolve:onecansimplyuseScipy'sinterfacetoODEPACK
(https://computation.llnl.gov/casc/odepack/odepack_home.html),anoptimizedFortranpackageforsolving
ordinarydifferentialequations.Here'showtheproblemcanbesetup:
import numpy as np
from scipy import integrate
# Note: t0 is required for the odeint function, though it's not used here.
def lorentz_deriv((x, y, z), t0, sigma=10., beta=8./3, rho=28.0):
"""Compute the time-derivative of a Lorenz system."""
return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]
x0 = [1, 1, 1] # starting vector
t = np.linspace(0, 3, 1000) # one thousand time steps
x_t = integrate.odeint(lorentz_deriv, x0, t)
That'sallthereistoit!
Visualizingtheresults
Nowthatwe'vecomputedtheseresults,wecanusematplotlib'sanimationand3Dplottingtoolkitstovisualizethe
trajectoriesofseveralparticles.BecauseI'vedescribedtheanimationtoolsindepthinapreviouspost
(/blog/2012/08/18/matplotlibanimationtutorial/),Iwillskipthatdiscussionhereandjumpstraightintothecode:
LorenzSystemlorentz_animation.pydownload(/downloads/code/lorentz_animation.py)
import numpy as np
from scipy import integrate
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import cnames
from matplotlib import animation
N_trajectories = 20
Theresultinganimationlookssomethinglikethis:
0:33
Noticethattherearetwolocationsinthespacethatseemtodrawinallpaths:thesearethesocalled"Lorenz
attractors",andhavesomeinterestingpropertieswhichyoucanreadaboutelsewhere.Thequalitative
characteristicsoftheseLorenzattractorsvaryinsomewhatsurprisingwaysastheparameters(, , ) are
changed.Ifyouaresoinclined,youmaywishtodownloadtheabovecodeandplaywiththesevaluestoseewhat
theresultslooklike.
IhopethatthisbriefexercisehasshownyouthepowerandflexibilityofPythonforunderstandingandvisualizing
alargearrayofproblems,andperhapsgivenyoutheinspirationtoexploresimilarproblems.
Happycoding!
PostedbyJakeVanderplasFeb16,2013
Tweet
21
Comments
21Comments
Recommend 2
PythonicPerambulations
Share
Login
SortbyBest
Jointhediscussion
tjc 2yearsago
Hi Jake,
I was a bit perplexed by the 'sum' on lines 35 and 37. Crashes as is, for me. Did you mean:
Reply Share
PrahasDavidNafissian>tjc 3monthsago
Absolutely correct correction! And on the Mac, take out the blit=true near the
bottom!
Reply Share
uriel_sandoval>tjc 2yearsago
Probably you are using a notebook with pylab inline, and that calls you the np.sum
function instead of the buit-in python sum.
Reply Share
BenRoot 2yearsago
Reply Share
jakevdp
Mod >BenRoot
2yearsago
Reply Share
GaelVaroquaux 2yearsago
Hey Jake,
Have you ever played with the Mayavi interactive examples of the Lorentz system:
http://docs.enthought.com/maya...
and
http://docs.enthought.com/maya...
I my opinion, what's really real with the second one, which is slightly more complex, is that
you can change parameters interactively and watch the system evolve.
1
Reply Share
jakevdp
Mod >GaelVaroquaux
2yearsago
Hey Gael,
Thanks for the links! Mayavi seems to have some pretty slick 3D tools! Maybe I'll
spend a little more time playing around with that.
Reply Share
PrahasDavidNafissian 3monthsago
Hello -- is there a way to generate a log file with the x,y,z points generated by this algorithm?
Thanks!
Reply Share
PrahasDavidNafissian 3monthsago
Reply Share
CescFangs 4monthsago
Hi Jake,
amazing tutorial!!
When i try to save the animation as mp4,i got "UserWarning: MovieWriter ffmpeg
unavailable", and im pretty sure the ffmpeg installation was successful, how can i deal with
it?
Reply Share
CescFangs>CescFangs 4monthsago
The thing is i can run it with ipy notebook, however when i use sublime text, the
error occurs, i think it's something like environment path problems
Reply Share
TimBurton ayearago
great animation...but when I tried to run it locally in IPython (with python 3.4) it erred out
on line 14 "invalid syntax" ???
Reply Share
eldada ayearago
Assume the `sum` function is not overloaded, which is often not the case in scientific
programming with python (e.g. %pylab). An alternative code which does not rely on the
`sum` function:
lines = [l for c in colors for l in ax.plot([], [], [], '-', c=c)]
pts = [pt for c in colors for pt in ax.plot([], [], [], 'o', c=c)]
Reply Share
jakevdp
Mod >eldada
ayearago
Thanks! The ``sum`` overloading is one of the many reasons that the IPython team
is deprecating %pylab in favor of %matplotlib, which doesn't do any silent
overwriting of built-in functions. I'd recommend using the latter in the future.
Reply Share
DavidP.Sanders 2yearsago
This is just amazingly beautiful! Probably the best animation out there of this stunning
object!
Let me comment on the mathematics:
You say that there are "two locations in the space that seem to draw in all paths". In your
animation it indeed seems that these two locations are in the center of some circle-like
regions.
There are indeed two special points at the centers of those regions, but they are *unstable*
fixed points, and hence are repellers, not attractors! Once a trajectory spends enough time
near one of those points, it will be ejected in a perpendicular direction and wind around the
two "lobes".
To see this, it's probably easier to run a single trajectory for more time. To *prove* it, of
course, is a different story (and actually very difficult -- it was done in 2002 by Warwick
Tucker).
In fact, each of those fixed points is (if I remember correctly) a so-called "saddle--focus".
"Focus" means that there is a two-dimensional object like a plane (the so-called "stable
manifold") where the trajectory approaches the fixed point asymptotically in a spiral; this is
seemore
Reply Share
DavidP.Sanders>DavidP.Sanders 2yearsago
Reply Share
jakevdp
Mod >DavidP.Sanders
2yearsago
Thanks for the info! I must admit I haven't looked closely at the theory
behind this... it's good to have my claims double-checked :)
Reply Share
tjc 2yearsago
Reply Share
CamDP 2yearsago
Reply Share
Lywx 2yearsago
Reply Share
Emi 2yearsago
Wonderful!!!! The students can now see the chaotic evolution of this system, and what
sensitive dependence on initial conditions means.
Reply Share
WHAT'STHIS?
ALSOONPYTHONICPERAMBULATIONS
IsSeattleReallySeeinganUptickIn
Cycling?
KernelDensityEstimationinPython
14commentsayearago
21comments2yearsago
ASimpleAnimation:TheMagicTriangle
Numbavs.Cython:Take2
6comments2yearsago
52comments2yearsago
Subscribe
AddDisqustoyoursite
Privacy
RecentPosts
FastLombScarglePeriodogramsinPython(https://jakevdp.github.io/blog/2015/06/13/lombscargleinpython/)
OptimizingPythonintheRealWorld:NumPy,Numba,andtheNUFFT
(https://jakevdp.github.io/blog/2015/02/24/optimizingpythonwithnumpyandnumba/)
TheHipsterEffect:AnIPythonInteractiveExploration(https://jakevdp.github.io/blog/2014/11/11/thehipster
effectinteractive/)
HowBadIsYourColormap?(https://jakevdp.github.io/blog/2014/10/16/howbadisyourcolormap/)
OnFrequentismandFriedChicken(https://jakevdp.github.io/blog/2014/09/02/onfrequentismandfriedchicken/)
Follow@jakevdp
5,389followers
Copyright20122015JakeVanderplasPoweredbyPelican(http://getpelican.com)