import numpy as np
import urllib # for reading information from a URL
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib notebook
Download the file test1.dat
from PHYS 310 website. Here's what's in the file:
# This line is a comment
#
#t x y
# ============
1.2 2.3 3.2
2.2 4.6 6.1
3.0 5.9 9.1
The file is in the format of a typical data file. Each line might be something like $(t,x,y)$.
Note that by default loadtxt
ignores any lines in the data file that begin
with the #
character. (This can be changed.)
a = np.loadtxt("test1.dat")
a
It's often useful to break the data into single arrays for $t$, $x$, and $y$. One way to do this is with
the transpose of a
:
a.T
t, x, y = a.T
t
or loadtxt
can get the columns of the data file directly:u
t, x, y = np.loadtxt("test1.dat", unpack=True)
Here's a copy of test2.csv
# This line is a comment
#
#t x y
# ============
, 1, 2, 3
comment in column 0, 2, 4, 6
, 3, 9, 27
b = np.loadtxt("test2.csv", delimiter = ',', usecols = (1,2,3), unpack = True)
b
c = 2*a
c
np.savetxt('testout1.dat', c, header='sample header (optional)' )
np.loadtxt()
has now been upgraded to read directly from a URL:
link = 'http://www.eg.bucknell.edu/~phys310/jupyter/test1.dat'
f = urllib.request.urlopen(link)
data = np.loadtxt(f)
print(data)
urllib.request.urlretrieve(' http://www.atlasoftheuniverse.com/nebulae/m42.jpg','orion.jpg')
img = mpimg.imread('orion.jpg')
plt.figure()
plt.imshow(img);
version_information
is from J.R. Johansson (jrjohansson at gmail.com); see Introduction to scientific computing with Python for more information and instructions for package installation.
version_information
is installed on the linux network at Bucknell
%load_ext version_information
%version_information numpy, matplotlib