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

    • is the simplest program to plot data from a file ( gauss.txt in this example). For conciseness, it uses pylab instead of explicit numpy, scipy, and matplotlib methods.
    • fits a gaussian to data in file gauss.txt , and can be easily modified to fit your own data and model.

      Slightly simpler or more complex versions are also available: and

    • fits data with uncertainties in both x and y, using Orthogonal Distance Regression (ODR). This is also useful when the data has only x uncertainties, but the fit function, f(x), is not analytically invertible or is not single valued in y. The residual variance from ODR is a "quasi χ 2 " which converges to the regular "χ 2 " for small x uncertainties, but note that if the x uncertainties are large, the cumulative probability distribution is better (but more slowly) estimated by the Monte Carlo option inside the program.

      Sample data files include gauss_dx.txt (which is identical to gauss.txt except for the addition of tiny x uncertainties), gauss_xy_errors_ugly.txt which illustrates how large x uncertainties can result in very large parameter uncertainties, and a straight line (linear_xy_errors.txt) and a semicircle (semicircle.txt).

    • extends the ODR fitter so it can fit data with systematic uncertainties or uncertainties that are not gaussian standard deviations. Monte Carlo methods are used to calculate the output parameter uncertainties and Confidence Interval for the fit. The example in the code fits a Gaussian to data in file gauss_ODR.txt . For more information, see the ODR Fitter Description.
    • is a simple fit and plot of a 2 dimensional distribution. It is a minor modification of a scipy example.
  2. - Makes one dimensional histogram of a list of numbers.

    • The resulting histogram is both displayed and saved as an image, and also output to a text file which can be input to a curve fitting program.
  3. - A simple example of how to use scipy.optimize.minimize

    • Several possible functions are provided, only one of which has a simple single minimum, to illustrate some simple pitfalls.
  4. - Converts a folder of images into a single compact pdf file. For example, photos of your lab notebook pages can be combined into a single file for electronic submission.

  5. - Converts thermocouple voltages into temperature, or vice versa.

    • See comments in code for currently supported thermocouple types.
    • is a crude quick test of
  6. - Calculates the magnetic field near the centre of Helmholtz coils.

  7. Tracking beads or other spots in a sequence of images can be done with code available from our Optical Tweezers experiment .

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

  9. Laue diffraction spots for a cubic crystal can be identified using .

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

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

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

    • is a simple example, but it can't be easily stopped until it finishes.
    • 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."
    • plots an exponentially decaying sinusoid with gaussian noise. It includes start and pause buttons, and is selective about what it plots.
  13. - 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 13 September 2020