# Python

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:

- Python Data Science Handbook
- Learn Python the Hard Way
- A Crash Course in Python for Scientists
- Software Carpentry
- Fun with Lists
- Python | Codecademy
- Scipy Lecture Notes
- A gallery of interesting IPython Notebooks
- Think Python: How to Think Like a Computer Scientist
- The Python Graph Gallery

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

My advice on learning Python:

— Jake VanderPlas (@jakevdp) September 10, 2017

Don't set out to "learn Python". Choose a problem you're interested in and learn to solve it with Python.

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.]

## Fitzhugh-Nagumo — Labyrinthine Patterns

Main features: how to make a movie, time-integration methods (semi-spectral and Euler)

## Conway’s Game of Life, acorn initial conditionnn

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

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

## Fun with histograms

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

## Fancy subplot grid

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`

## Streamplot

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

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

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

Construction of the Hilbert curve as a Lindenmayer system (L-system).

Main features:
`string operations, movie`

## A hysteresis mechanism

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 parameter $h$ is ramped down and up during this simulation.

Main features:
`sympy analytical calculations, numpy dtypes, movie`

## Contour plot

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)`

## Least action principle on the beach

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`

## This website’s logo

Simple example of how to make a figure without any visible axes.

Main features:
`set_axis_off, fill_between, matplotlib inline plot on Jupyter`

## Bars

Horizontal and vertical bars, with numeric legends. Unicode support.

Main features:
`unicode, bar, barh, grid`