Description
Python Reminders
Last year, many of you will have used Python for a variety of tasks. Importantly among these
was reading in data, plotting them, and being able to fit functions to them along with obtaining
uncertainties in those fits. It is not a strict requirement to make use of Python for the work in
PH500/PH520. However, it is strongly encouraged as being among the best ways to approach the data
processing, analysis and plotting. The following sections show reminders and quick examples of some
of the key tasks for data processing in lab.
Section 1: Reading in data
Data can be read in using one of two main methods covered in PH370 (there are other ways but
they are not elaborated here). The most useful in the majority of circumstances is the genfromtxt
and/or loadtxt functions provided by the NumPy module. For a plain text data file, containing three
columns of numbers with a ‘header’ line at the top, you might have this as the file, named ‘mydata.csv’,
for example:
The first line loads the NumPy module, the second reads the data in. The first variable name
times will contain every number from the first column, volts every number from the second, etc.
The arguments to the genfromtxt() function are as follows:
• Initially, the name of the file to open.
• skip_header= requires an integer value; how many lines at the top of the file to ignore before
reading numbers.
• delimiter= requires a string; indicates what symbol separates the columns, in this example it
was a single comma. Leaving this out will automatically split at any ‘whitespace’ (spaces, tabs,
etc.)
• unpack= requires a boolean (True or False); indicates whether the break the columns apart to
store in separate variables; if False a single, 2D array is returned, instead of multiple 1D arrays.
• usecols= requires a list of integers; indicates which columns (numbered from 0 up) to use. The
above example uses all three, but one could just list times,counts at the start of the line, and
then have [0,2] as the argument to this option.