Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Jupyter notebook LabNotebook-Example/LabNotebook-Example.ipynb

276 views
Kernel: Python 3 (Ubuntu Linux)

Lab Notebook - Example

Alan DeWeerd (1/2/17)

Notes

Notes can be written in a "Raw Text" cell.

You can also enter a superset of HTML in a "Markdown" cell, which gives more control over formatting and tends to look better. (See basic commands and syntax). Double-click this cell to edit it.

Headings

Headings are very useful for labeling sections. There are no longer "Heading" cells. To make a heading (like above), use a "Markdown" cell and start with "#" for a top level heading, "##" for a second level heading, etc. Note that you must leave a space between "#" and the heading text.

Equations

Equations can also be included using LaTeX commands. There is a brief tutorial on typesetting math with LaTeX, which has a link to more documentation. In a "Markdown" cell, LaTeX commands between dollar signs ($) appear inline with text and LaTeX commands between pairs of dollar signs ($$) appear on a separate line (called a display equation). When you run the cell to display the equation, the latex command is hidden. The LaTeX commands can be edited by double clicking on the cell.

An example of an inline equation is F=maF=ma, and a display equation is a=dvdt.a=\frac{dv}{dt}.

Images

If you import the "Image" function (only needs to be done once), it can be used to include .jpg or .png files. Image files should be in the same folder as the notebook. The "width" argument can be used to adjust the size of the image.

from IPython.display import Image
Image(filename="Fig1-1E.jpg",width=500)
Image in a Jupyter notebook

You can using a drawing program, such as "Paint" on Windows, to make simple notations on a figure before including it.

Calculations

You can use "Code" cells to perform calculations. This works well because it shows how you did the calculation. For example:

from math import * a = 3 b = 4 x = sqrt(a**2 + b**2) print(x)
5.0

For calculations, you should:

1. Use descriptive variable names 2. Express all numerical quantities in standard units (Ex: ohms, not kilo-ohms). 3. Make a note of what the units are.

Data Tables

If you import the "panda" package (only needs to be done once), you can read an ASCII data file and display the contents in a table.

from pandas import *

The default for pandas is comma seperated values. The first line should contain headers (also comma separated) for the columns on the lines below. The file "test.dat" contains the following:

# t (s), V (mV) 0.1, 1 0.2, 2 0.3, 4

You can make the "read_csv" command ignore the number symbol (#) using the "escapechar" argument. The "#" at the start of the header line is useful because it is easy to make the "loadtxt" command (see the graphing example below) treat it as a comment. The row numbers (starting with zero) always appear in the first column of the table.

read_csv("test.dat",escapechar="#")

Often data is seperated by spaces, not commas. The file "test2.dat" contains the following:

# t(s) V(mV) 0.1 1 0.2 2 0.3 4

In this case, the "read_csv" has to be told that the separators are spaces. It's simpler to leave the spaces out of the column headers.

read_csv("test2.dat",sep=" ",escapechar="#")

You should give your files more descriptive names. You can also add extra comment lines at the top of file and have "read_csv" ignore them using the "skiprows" arugment. The file "test3.dat" contains the following:

\# Measurements for charging a capacitor in an RC circuit \# The time is measured from when the switch was closed \# R = 0.97 k-ohm, C = 0.1 micro-F \# t(s) V(mV) 0.1 1 0.2 2 0.3 4
read_csv("test3.dat",skiprows=3,sep=" ",escapechar="#")

Graphs

Often, you'll want to make a plot of data that you've taken. There are tutorials on reading data files and making plots with Python.

The example below shows how to read in data and make a scatter plot of it. Note that the "loadtxt" command, which is part of the "numpy" library, expects spaces between entries as in "test2.dat". It also ignores all lines that start with "#". The graphing commands in the "pylab" library.

from numpy import * from pylab import * t,V = loadtxt('test2.dat',unpack=True) figure() scatter(t,V) xlabel('t (s)') ylabel('V (mV)') xlim(0) ylim(0) show()
Image in a Jupyter notebook

For the comma separated values in "test.dat", the "delimiter" argument must be used to make the "loadtxt" command treat commas as the separators. Note that you don't really need to import the libraries a second time in a Jupyter notebook.

from numpy import * from pylab import * t, V = loadtxt('test.dat',delimiter=',',unpack=True) figure() scatter(t,V) xlabel('t (s)') ylabel('V (mV)') xlim(0) ylim(0) show()
Image in a Jupyter notebook

It is often useful to plot data points and a theoretical funciton in the same figure as shown below. Suppose that you expect that data to be described by the equation V=0.5+40t2.V = 0.5 + 40t^2. The following example plots both the data, as a scatter plot, and the theoretical equation, as a curve.

from numpy import * from pylab import * t,V = loadtxt('test2.dat',unpack=True) t2 = linspace(0,0.35,100) V2 = 0.5 + 40*t2**2 figure() scatter(t,V,c='b',label='Data') plot(t2,V2,c='g',label='Theory') xlabel('t (s)') ylabel('V (mV)') xlim(0) ylim(0) legend(loc='upper left') show()
Image in a Jupyter notebook