numpy
, and matplotlib
examples¶Marty Ligare. August 2020
This is a markdown cell. Markdown cells are for for formatted comments. See the Markdown Cheat-Sheet for more information.
If you single-click on a cell you can get a box around the cell, and you can see what kind of cell it is in the menu bar above (Markdown, code, etc.), and you can change it.
Double click on this cell, and we'll make some changes. Let's add math: $e^{i\pi} = -1$
Shift+Enter
to execute cells.
Functions from modules imported in this manner must be preceded by the "name" given to the module (np
, plt
), e.g., np.zeros()
below.
import numpy as np
import matplotlib.pyplot as plt
The following is an Ipython
"magic" command that places plots into notebooks,
and makes the plots interactive.
%matplotlib notebook
for i in range(5):
print(i)
for i in range(4,10):
print(i)
for i in range(4,10,2):
print(i)
i = -2
while i < 5:
print(i)
i += 1
i = 5
if i<4:
print("ok")
elif i == 4:
print("better")
else:
print('not ok')
Numpy arrays are easy-to-use "containers" for data that are computationally efficient.
a = np.array([10, 20, 30, 4.e1])
print(a)
b = np.zeros(10)
print(b)
b = np.zeros((10,10))
print(b)
c = np.eye(4,4)
print(c)
Table
command)¶d = np.array([i*i for i in range(100) if i%2==1])
print(d)
linspace
function¶Especially useful in making points along independent axis in graphs.
np.linspace(4,6,11)
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
a**2
*
gives element-by-element multiplication¶a*b
@
operator gives matrix multiplication (here, the dot product)¶a@b
a[0], a[-1]
:
)¶a[1:], a[:3]
c = np.array([a,b])
print(c)
c
(Take the transpose of c
and extract 2nd element)¶np.transpose(c)[1]
def f(x,y):
'''Comments about a function go here; they are called a 'docstring'.
This function returns the product of x and y.'''
return x*y
f(3,4)
docstring
¶f?
np.linspace?
a = np.linspace(1,10,10)
f(a,.5)
plt.figure()
x = np.linspace(0,5,201) # Make a pseudo-continuous set of x-values
y = x**2 # y values for smooth function
plt.plot(x,y)
xdata = np.array([1,2,3,4,5])# Discrete x-values
ydata = xdata**2 # y-values for discrete data set
plt.scatter(xdata,ydata);
plt.figure()
plt.axhline(0, color='magenta')
plt.axvline(0, color='magenta')
plt.title("My theory")
plt.xlabel('$x$')
plt.ylabel('$\sigma$')
plt.grid()
x = np.linspace(0,5,201)
y = x**2
plt.plot(x,y, label="theory")
xdata = np.array([1,2,3,4,5])
ydata = xdata**2
plt.scatter(xdata,ydata)
plt.legend()
plt.xlim(-2,7);
plt.figure()
plt.axhline(0, color='magenta')
plt.axvline(0, color='magenta')
plt.title("My theory")
plt.xlabel('$x$')
plt.ylabel('$\sigma$')
plt.grid()
x = np.linspace(0,5,201)
y = x**2
plt.plot(x,y, label="theory")
xdata = np.array([1,2,3,4,5])
ydata = np.array([1.3, 3.5, 9.4, 15.5, 24])
u = np.ones(len(xdata))*0.8
plt.errorbar(xdata, ydata, yerr=u, fmt='o',label='data')
plt.legend()
plt.xlim(-2,8);
a = np.loadtxt('sample.dat')
print(a)
b = a**2
print(b)
np.savetxt('output.dat',b)
version_information
is from J.R. Johansson (jrjohansson at gmail.com);
see Introduction to scientific computing with Python:
http://nbviewer.jupyter.org/github/jrjohansson/scientific-python-lectures/blob/master/Lecture-0-Scientific-Computing-with-Python.ipynb
for more information and instructions for package installation.
If version_information has been installed system wide (as it has been on Bucknell linux computers with shared file systems), continue with next cell as written. If not, comment out top line in next cell and uncomment the second line.
%load_ext version_information
version_information numpy, matplotlib