Python is a great language for scientific computing, most of the programming done by our group is in python. We provide below some links for learning this language, and below we offer many python code examples. You are invited to download these codes, tweak with them, break them, hack them as you wish!

Most codes focus on plotting, but other algorithms such as numerical integration and Fourier transforms can also be found.

Some useful links for learning python:

This might be overwhelming, so I suggest you to follow this:

Start by downloading Anaconda, a package manager application that will help you get started with python in all platforms. ​ Make sure you are acquainted with ipython (interactive python, try it here), and with Jupyter notebook. ​ I recommend Sublime Text for writing code, and you can install the Anaconda package to it to have a smooth IDE. [not the same anaconda as mentioned above! Yes, it’s confusing, I know.] lets you run python scripts online! Run () this, and edit it as you wish:

Conway’s Game of Life, acorn initial condition

Jupyter notebook

This is a (slightly) modified version of Glowing Python’s code. I make it available here because it features a few nice things:

  • how to make a movie using matplotlib.animation
  • how to write a generator (function with yield)
  • how to plot a sparce array (spy)

Main features: matplotlib.animation, yield, with, matplotlib.pyplot.spy

Least squares fit of nonlinear function

Jupyter notebook

Main features: LaTeX text, scipy.optimize.curve_fit, matplotlib.patches

Fun with histograms

Jupyter notebook

Main features: np.histogram, plt.hist,, plt.barh, gridspec, least squares fit of nonlinear function, plt.hist2d

Fancy subplot grid

Jupyter notebook

Highly customizable subplot structure. Also, figure contains several axis configurations and labeling options.
Main features: gridspec subplots; numpy-compatible heaviside; label, ticks and axis manipulations; log scale


Jupyter notebook

Streamplot of a two-dimensional linear system, with eigenvectors and nullclines. Python shows LaTeX equations beautifully.
Main features: meshgrid, streamplot, contour, legend, LaTeX

The time dependent Ginzburg-Landau equation

Jupyter notebook

Numerical integration of a parabolic partial differential equation, using finite differences: Euler step to advance time, and a 5-point stencil to approximate the Laplacian.
Main features: imshow, colorbar, set_data

The double pendulum

Jupyter notebook

Numerical integration of the equations of motion of the double pendulum. This time, scipy’s ode itegrator was used. Nice example of how to make a movie.
Main features: scipy.integrate.ode, set_data, set_aspect('equal'), remove plot, movie

The Hilbert curve

Jupyter notebook

Construction of the Hilbert curve as a Lindenmayer system (L-system).
Main features: string operations, movie

A hysteresis mechanism

Jupyter notebook

Hysteresis mechanism created by the bistability of states. System goes to minimum points u in the energy functional f=u^4-2u^2+hu. The the parameter h is ramped down and up during this simulation.
Main features: sympy analytical calculations, numpy dtypes, movie

Contour plot

Jupyter notebook

Contour plot with many customizable options. Also, a nice way to truncate a colormap so it gives the color range that you want.
Main features: truncate_colormap, contour, contourf (fill), clabel (contour label)

Drowning person problem

Jupyter notebook

How should ​lifeguard run in order to save a drowning person in minimal time? Answer: by using Snell’s law of refraction!
This is a nice example how to use spines (x and y axis form a cross), instead of rectangular figures as usual. Also, “annotations” are used, where things can be labeled with the help of arrows.
Main features: spines, matplotlib.patches.Rectangle, annotate

Jupyter notebook

Simple example of how to make a figure without any visible axes.
Main features: set_axis_off, fill_between, matplotlib inline plot on Jupyter


Jupyter notebook

Horizontal and vertical bars, with numeric legends. Unicode support.
Main features: unicode, bar, barh, grid