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.
There are many Python installations available, but scientific users require several packages that are not always included.
The SciPy Cookbook for Fitting Data has multiple examples.
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.
Data Plotting and Fitting for (x,y) data :
Slightly simpler or more complex versions are also available: simple_curve_fit_to_data.py and extended_curve_fit_to_data.py.
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).
histogram_1D.py - Makes one dimensional histogram of a list of numbers.
minimize_example.py - A simple example of how to use scipy.optimize.minimize
Image_folder_to_PDF.py - 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.
thermocouple.py - Converts thermocouple voltages into temperature, or vice versa.
helmholtz.py - Calculates the magnetic field near the centre of Helmholtz coils.
Tracking beads or other spots in a sequence of images can be done with code available from our Optical Tweezers experiment .
Unknotting and motion of a beaded chain on a vibrating plate can be studied with code available from our Unknotting experiment .
Laue diffraction spots for a cubic crystal can be identified using Laue_Spot_Identify.py .
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.
stopwatcher.py - Record time of keystrokes or position/time of cursor clicks, and record them to both a file and the python terminal.
Animated plot examples, expanded from a MatPlotLib example :
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 13 September 2020