Advanced Undergraduate Laboratory

Department of Physics

University of Toronto

Python for the Advanced Physics Lab

Python is a widely used programming language with many open source tools and libraries. It is not as computationally fast as pure compiled languages such as FORTRAN or C++, but it is generally considered easier to learn. Python is the preferred beginning language for undergraduate physics computation at the University of Toronto, both in lab and lecture courses. Python is free and can be installed on almost any computer.

Installing Python on your Computer

There are many Python installations available, but scientific users require several packages that are not always included.

Experimental Physics Using Python

The SciPy Cookbook for Fitting Data has multiple examples.


Python Code Repository

These python programs have been developed, modified, or used in the Advanced Physics Lab for fitting, numerical calculation, simulation, and video analysis.

These programs should run on Python 2.6 or 2.7, and usually require numpy, scipy, matplotlib. In some cases, as noted, they may require the Python Imaging Library (PIL), OpenCV, or VPython. In order to help infrequent or new Python users, the programs are usually over-commented. If you find a bug in one of these programs, please let the Coordinator know.

  1. Data Plotting and Fitting for (x,y) data:

  2. helmholtz.py - Calculates the magnetic field near the centre of Helmholtz coils.

  3. thermocouple.py - Converts thermocouple voltages into temperature, or vice versa.

    • See comments in code for currently supported thermocouple types.
    • thermocouple_test.py is a crude quick test of thermocouple.py.
  4. Tracking beads or other spots in a sequence of images can be done with code available from our Optical Tweezers experiment.

  5. Unknotting and motion of a beaded chain on a vibrating plate can be studied with code available from our Unknotting experiment.

  6. driven_bouncing_balls.py is a vPython simulation of 3 balls bouncing vertically on an oscillating plate. States from phase stability to chaos can be studied.

  7. stopwatcher.py - Record time of keystrokes or position/time of cursor clicks, and record them to both a file and the python terminal.

  8. Animated plot examples, expanded from a MatPlotLib example:

    • Live_Plot_Simple.py is a simple example, but it can't be easily stopped until it finishes.
    • vonNeumann_elephant.py demonstrates the famous dictum attributed to John von Neumann that: "With four parameters I can fit an elephant, and with five I can make him wiggle his trunk."
    • Live_Plot.py plots an exponentially decaying sinusoid with gaussian noise. It includes start and pause buttons, and is selective about what it plots.
  9. FaceDetect.py - A simple example of real-time webcam face-recognition using OpenCV. Because of a bug in OpenCV, it may be necessary to "Force Quit" Python to close the camera window when the program finishes.

Last updated on 25 September 2012