Navigation tip:click on the Document pull-down menu (upper left corner) to reveal a table of contents for this article.
Introduction
What this is
This is a short guide to using Python to accomplish some commonly needed tasks when working with data acquired in an experimental physics lab, including data import/export, plotting with error bars, curve fitting of functions to data, testing the goodness of fit of these functions to the data (taking into account the uncertainty in the measurements), the interpolation, smoothing, and differentiation of data , the propagation of "error" in calculated quantities, and numerical calculations of data including units and/or uncertainty. A careful discussion of how to import needed functions from the Numerical Python (numpy) and Scientific Python (SciPy) libraries — what Python calls 'packages' — is also included. I rely heavily on functions from these packages to simplify various analysis tasks in the examples below. The focus here is on using Python as a scientific and graphing calculator for experimental data, but more experienced Python users should still find the functions and methods presented here useful for their own programming tasks.
My goal is for you to be carry out each of the analysis steps illustrated here using your own data, and to be able to do so by making at most only a few small changes (such as the names of data files and columns of data) to the templates included with each example. Beginners will need to know a few basic Python rules and syntax — for example, that \(x^2\) is written x**2
and NOT as x^2
— but will not need advanced programming skills. Recommendations on how to get started with Python are included below.
How to get started
Python
If Python is new to you, I recommend starting by reading the very well written chapter
Python programming for physicists from the Python-based textbook
Computational Physics by Mark Newman, which gives "an introduction to the Python language at a level suitable for readers with no previous programming experience"
\cite{mark2013}.
Jupyter Notebooks
The examples provided in this guide run Python within user friendly but somewhat oddly named
Jupyter notebooks. In some ways they look and act like Mathematica® notebooks, including the use of 'cells' (paragraphs of text, equations, or code) and the use of the
shift+enter
keys to execute the code in a cell. Jupyter notebooks are a spin-off of what is known as interactive Python (iPython) and in older examples on the web, you will still encounter references to iPython Notebook instead of Jupyter. This is also why Jupyter is spelled as it is (with a 'py') and why Jupyter notebook filenames end with
.ipynb
.
Finally, note that Jupyter notebooks are not the only way to write and run Python code — the examples provided in the text
Computational Physics use a much more bare-bones IDLE command line editor (familiar to many beginning computer science students) and advanced Python programmers often prefer a MATLAB® style interface such as
Spyder (again with the 'py'!) that combines an editor for writing Python programs (ending in .py) with an interactive Python command line and debugging tools— and the Python examples provided here will run in any of these environments. But this also means you can run Python examples provided in
Computational Physics and elsewhere within your own Jupyter notebooks!
Try it out!
On a webserver
Jupyter notebooks are viewed and run using a web browser such as
Firefox (just like this article in
Authorea). That means they can also be run on a webserver that you access from a web browser and, as a result, it isn't necessary to have Python installed on your own computer (provided you have internet access and all the Python packages you wish to use are already installed on the webserver).
To try out the Python programs presented here
using a Jupyter webserver hosted by Authorea, click the
</> Code
button found to the left of many of the figures in this guide. This will reveal the
.ipynb
Jupyter notebook (and associated data files) containing the Python code used to generate that figure. Clicking on the notebook file name will launch the notebook in a new tab or window within your web browser . Clicking within a 'cell' (a block of text, equations, and or code, outlined by a rectangular border) and hitting SHIFT-ENTER will run any code in that cell and advance to the next one. Alternatively, you can make edits to the code in notebook (for example, adjust a smoothing parameter or change the name of a file) then select
Run All
from the
Cell
menu or
Restart & Run All
from the
Kernel
menu to rerun the program. For additional help, see the
online interactive tutorial on how to run Python inside Jupyter notebooks.
Many schools
host and configure their own Jupyter webservers for Python programming by their students. For example,
Smith College physics students can upload and run any of the Jupyter notebooks included in this guide on the webserver
https://jove.smith.edu, as this particular webserver has all the packages used here preinstalled. (Note to Smith students: the https is required. Contact the course instructor for an account).