# Compatibility with Python 3
from __future__ import print_function # print('me') instead of print 'me'
from __future__ import division # 1/2 == 0.5, not 0
# Show figures inside the notebook
%matplotlib inline
import numpy as np # the Python array package
import matplotlib.pyplot as plt # the Python plotting package
We have already seen how to slice arrays using colons and integers.
The colon means 'all the elements on this axis':
an_array = np.array([[0, 1, 2, 3], [4, 5, 6, 7]])
an_array
an_array[:, 1] # All rows, only the second column
an_array[0, 1:] # Only the first row, all columns except the first
We have also seen how to slice using a boolean array the same shape as the original:
is_gt_5 = an_array > 5
is_gt_5
an_array[is_gt_5] # Select elements greater than 5 into 1D array
We can also use boolean vectors to select elements on a particular axis. So, for example, let's say we want the first and last elements on the second axis. We can use a boolean vector to select these elements from a particular axis, while still using integer and colon syntax for the other axes:
want_first_last = np.array([True, False, False, True])
an_array[:, want_first_last] # All rows, columns as identified by boolean vector
So far we have done very simple line-plots with the defaults.
Let's say we are plotting some points with matplotlib. That might look like this:
x = np.arange(0, np.pi * 2, 0.2) # The x axis values for the plot
y = np.sin(x) # The y axis values
plt.plot(x, y)
By default matplotlib will join up the neighboring points with lines. We can also ask matplotlib to show the individual points. This is useful for scatterplots.
To do this, can you specify a 'marker'. For example, let's say you wanted to use a '+' marker:
plt.plot(x, y, '+')
# Or a circle marker
plt.plot(x, y, 'o')
It is often helpful to see images or plots side-by-side rather than in separate windows (or cells in the IPython notebook).
Matplotlib uses the subplots command for this.
To make subplots, you use the subplots command to specify your grid of plots (number of rows in your grid, number of columns). Each element of the grid has its own "axis" object. You can use the axis object methods to do plots and image displays on the individual axes.
# Here I make a 1 row, 2 column grid
fig, axes = plt.subplots(1, 2)
# Axes are the individual plots, with their own - axes
# matplotlib returned 2 axes for us.
print('Shape of axes is', axes.shape)
# Now I can use the axes objects to do the plots
axes[0].plot(x, y)
axes[1].plot(x, y, 'o')
If you need help, try plt.subplots? in IPython. You can also tab complete on the axis objects like this:
axis0 = axes[0]
# Now you can tab complete on the axis object with:
# axis0.
It's common to need to create new arrays filled with zeros or ones, and Numpy (like MATLAB) has special commands for these:
np.zeros(5)
np.zeros((2, 5))
np.ones(3)
np.ones((3, 4))
Remember that Numpy, by default, will run functions across every element of the array without regard to the shape:
an_array
an_array.mean()
If you want Numpy to run functions across a particular axis, you need the axis keyword:
np.mean(an_array, axis=0)
np.mean(an_array, axis=1)