\ref{344573} Plotting Data Using Error Bars
\ref{822768} Curve FItting
Introduction
About this guide
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. The examples presented here 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 in their own programs.
The goal of this guide 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 to already be experienced programmers. For Recommendations on how to get started with Python, see below.
How to get started
Python
If Python is new to you, we recommend the
getting started with Python tutorials developed by our friends and rivals in the Wellesley College Physics Department. Go
Smith College Pioneers! These highly focused, highly practical tutorials are in the form of interactive Jupyter notebooks (see below) that allow you to try Python out as you learn; we use them in our own courses and they form the foundation for the methods presented here. We also recommend the unusually lucidly written (and most generously, freely provided) introductory chapter
Python programming for physicists from Mark Newman's Numerical Python-based textbook
Computational Physics \cite{mark2013}. Newman's text provides "an introduction to the Python language at a level suitable for readers with no previous programming experience" with an emphasis on the application of computational methods to typical theoretical problems in undergraduate physics.
Jupyter Notebooks
The examples provided in this guide run Python within user friendly but somewhat oddly named
Jupyter notebooks mentioned above. 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 with a 'py' and why Jupyter notebook filenames still end with
.ipynb
.
Note that Jupyter notebooks are not the only way to write and run Python code. A bare-bones command line editor called IDLE is used by many beginning computer science students (and in the text
Computational Physics) and a MATLAB® style interface called
Spyder (again with the 'py'!) is used by many advanced Python programmers. The Python programs provided here will run in any of these environments. But this also means you can run Python programs encountered elsewhere within Jupyter notebooks!
Within the Physics Department here at Smith College, we use the Jupyter notebook interface for two primary reasons: (1) running Python within the notebook provides a reproducible, self-documenting method of analyzing collected data, and (2) the ability to add explanatory notes, tables, figures, and equations within Jupyter notebooks means we can also use them as electronic lab notebooks for courses.
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 institutions
host and configure their own Jupyter webservers suitable for Python programming. 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: contact the course instructor for an account).