Getting started with Python: NumPy and Matplotlib

IPython Interactive Computing and Visualization Cookbook, http://ipython-books.github.io

Import packages

In [1]:
import numpy as np

%matplotlib inline
import matplotlib
from matplotlib import pyplot as plt
from matplotlib import ticker

NumPy

The package that make scientific use of Python possible

http://www.numpy.org

Main class: N-dimensional array ndarray

In [4]:
a = np.arange(10).reshape(2,5)
a
Out[4]:
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

Attributes:

In [5]:
a.shape
Out[5]:
(2, 5)
In [6]:
a.ndim
Out[6]:
2
In [7]:
a.dtype
Out[7]:
dtype('int64')

Array creation:

In [8]:
b = np.array([1,2,3,4,5])
b
Out[8]:
array([1, 2, 3, 4, 5])

Slicing

In [8]:
b[1:3]
Out[8]:
array([2, 3])
In [9]:
b[1:4:2]
Out[9]:
array([2, 4])
In [9]:
b[1::2]
Out[9]:
array([2, 4])
In [10]:
a[0]
Out[10]:
array([0, 1, 2, 3, 4])
In [11]:
a[:,0]
Out[11]:
array([0, 5])
In [12]:
c = a[:,:,np.newaxis]
c.shape
Out[12]:
(2, 5, 1)
In [14]:
a[:,[0,2]]
Out[14]:
array([[0, 2],
       [5, 7]])
In [15]:
x = (a % 2 == 0)
x
Out[15]:
array([[ True, False,  True, False,  True],
       [False,  True, False,  True, False]])
In [16]:
a[x] = 0
a
Out[16]:
array([[0, 1, 0, 3, 0],
       [5, 0, 7, 0, 9]])

Operations

In [17]:
d = 2 * a
d
Out[17]:
array([[ 0,  2,  0,  6,  0],
       [10,  0, 14,  0, 18]])
In [18]:
a + d
Out[18]:
array([[ 0,  3,  0,  9,  0],
       [15,  0, 21,  0, 27]])
In [19]:
a * d
Out[19]:
array([[  0,   2,   0,  18,   0],
       [ 50,   0,  98,   0, 162]])
In [20]:
a.dot(d.T)
Out[20]:
array([[ 20,   0],
       [  0, 310]])
In [21]:
np.dot(a, d.T)
Out[21]:
array([[ 20,   0],
       [  0, 310]])
In [22]:
a**2
Out[22]:
array([[ 0,  1,  0,  9,  0],
       [25,  0, 49,  0, 81]])

Functions

NumPy has redefined many mathematical functions to operate on arrays

In [23]:
np.sin(a)
Out[23]:
array([[ 0.        ,  0.84147098,  0.        ,  0.14112001,  0.        ],
       [-0.95892427,  0.        ,  0.6569866 ,  0.        ,  0.41211849]])
In [24]:
a.sum()
Out[24]:
25
In [25]:
a.sum(axis=0)
Out[25]:
array([5, 1, 7, 3, 9])
In [26]:
a.sum(axis=1)
Out[26]:
array([ 4, 21])
In [25]:
z = np.arange(2*3*4).reshape(2,3,4)
z
Out[25]:
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
In [26]:
z.sum(axis=1)
Out[26]:
array([[12, 15, 18, 21],
       [48, 51, 54, 57]])
In [28]:
a.sum(axis=1, keepdims=True)
Out[28]:
array([[ 4],
       [21]])

Broadcasting

In [28]:
a + 2
Out[28]:
array([[ 2,  3,  2,  5,  2],
       [ 7,  2,  9,  2, 11]])
In [29]:
e = np.array([1, 2]).reshape(2,1)
e
Out[29]:
array([[1],
       [2]])
In [30]:
a + e
Out[30]:
array([[ 1,  2,  1,  4,  1],
       [ 7,  2,  9,  2, 11]])
In [31]:
b
Out[31]:
array([1, 2, 3, 4, 5])
In [32]:
a + b
Out[32]:
array([[ 1,  3,  3,  7,  5],
       [ 6,  2, 10,  4, 14]])

Do not write for loops in Python!

Matplotlib

The default plotting library in Python

https://matplotlib.org

There are others.

Matplotlib has two types of interfaces:

  1. similar to MATLAB
  2. object-oriented

MATLAB-like interface

Module matplotlib.pyplot

Various states are preserved across function calls

In [34]:
x = np.linspace(0, 20, 100)
y = np.sin(x)

plt.plot(x, y, 'ro-')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Sin')
plt.grid(True);
In [35]:
plt.plot(x, np.sin(x), 'r', x, np.cos(x), 'b');
In [36]:
s = np.abs(np.random.randn(x.shape[0])) * 100
plt.scatter(x, y, c=y, s=s);
In [42]:
plt.subplot(1, 2, 1)
plt.plot(x, np.sin(x), 'r')
plt.subplot(1, 2, 2)
plt.plot(x, np.cos(x), 'b')
plt.subplots_adjust(wspace=0.4);
In [40]:
plt.plot(x, np.exp(-x))
plt.yscale('log')
plt.grid();

Oject-oriented interface

  • Pro: more flexible
  • Cons: more typing
In [44]:
fig, ax = plt.subplots()
ax.plot(x, y, 'ro-')

ax.set_xlabel('x')
ax.set_ylabel('y', rotation=0)
ax.grid()
ax.set_title('Sin');

fig.savefig('sin.eps', bbox_inches='tight')

Miscelaneous

Images

In [46]:
img = matplotlib.image.imread('sin.png')
In [47]:
type(img)
Out[47]:
numpy.ndarray
In [51]:
plt.imshow(img, interpolation='bicubic')
plt.axis('off')
plt.gcf().set_size_inches(8, 8);

Matrices

In [52]:
a = np.random.randn(4, 4)
a
Out[52]:
array([[ 7.59846689e-01,  4.78499043e-01, -1.49944941e-03,
        -7.60626669e-01],
       [ 1.61973467e+00,  9.32740568e-01,  1.15100017e-01,
        -2.18154489e-01],
       [ 1.05839602e+00, -3.94105525e-01, -4.36019828e-01,
         1.13151582e+00],
       [-4.22691639e-01,  1.09264556e-01,  7.58056507e-01,
        -2.30172963e-01]])
In [53]:
plt.matshow(a);

Customising tick marks

In [54]:
plt.style.use('ggplot')

fig, ax = plt.subplots()
ax.plot(x, y, 'r.-')

ax.set_xlabel('x')
ax.set_ylabel('y', rotation=0)
ax.grid(True)
ax.set_title('Sin');

ax.xaxis.set_major_locator(ticker.MultipleLocator(5.0))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(1.0))

ax.yaxis.set_major_locator(ticker.MultipleLocator(0.4))
ax.yaxis.set_minor_locator(ticker.MultipleLocator(0.2))