NumPy and Matplotlib Tutorial


In [ ]:
from __future__ import print_function
from numpy import *
from matplotlib.pylab import *
%pylab --no-import-all inline

NumPy Arrays

Creation


In [ ]:
a1 = array([1.0, 2.0, 3.0])
a2 = arange(1.0, 5.0, 0.5)
a3 = linspace(1.0, 10.0, 17)
print(a1)
print(a2)
print(a3)

In [ ]:
m1 = array([[1.0, 2.0], 
            [3.0, 4.0]])
print(m1)

In [ ]:
a4 = ones(10)
m2 = zeros((5,5))
a5 = empty(10)
print(a4)
print(m2)
print(a5)

In [ ]:
a7 = ones_like(a1)
m2 = zeros_like(m1)
# zeros_like, empty_like
print(a7)
print(m2)

In [ ]:
print(a7.shape)
print(m1.shape)
  • Create arrays with array, ones, zeros, empty.
  • Create 1D-arrays with arange, linspace, logspace.
  • Create arrays/matrices with the same shape as another with zeros_like, ones_like and empty_like.
  • Get the shape of an array with a.shape (not a function!)

Access


In [ ]:
print(a1[1])
print(a2[1:7])
print(a3[2::3])

In [ ]:
print(a2)
a2[3] = 7.0
print(a2)
a2[3:5] = 2.0
print(a2)

In [ ]:
m3 = zeros((10,10))
m3[5,:] = arange(0.0, 10.0)
print(m3)
m3[2:4,:] = 42.0
print(m3)
m3[3:9, 5:7] = 23.0
print(m3)

In [ ]:
m4 = ones((10,10))
m4[ [2,7],: ] = 42.0
m4[ :, [3,4] ] = 42.0
print(m4)

In [ ]:
m4[5,:] = 23.0
print(m4)
# swap lines!
m4[ [5,2],: ] = m4[ [2,5],: ]
print(m4)
  • Arrays can be accessed like lists (index, slicing).
  • Slices can be written to!
  • Can only contain objects of the same type.
  • Can not change the shape.

Array Math


In [ ]:
a1 = ones((10,10))
a2 = a1 + a1
print(a2)
a3 = 2*a2 - a1/2.
print(a3)

In [ ]:
print(sin(2.0))
xs = linspace(0.0, 2*pi, 10)
print(sin(xs))

In [ ]:
def f(x):
    return x**2 + 1.0

print(f(2.0))
print(f(2*ones(10)))
  • Math operations and functions in NumPy all work elementwise with arrays.
  • Own functions can be written to support both numbers and numpy arrays!

More NumPy functions

  • NumPy contains various functions to work with arrays (sorting, FFT, ...)

Matplotlib

Simple Plots


In [1]:
xs = linspace(0.0, 2*pi, 100)
plot(sin(xs))

figure()
plot(xs, sin(xs))


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-2da05c6f696a> in <module>()
----> 1 xs = linspace(0.0, 2*pi, 100)
      2 plot(sin(xs))
      3 
      4 figure()
      5 plot(xs, sin(xs))

NameError: name 'linspace' is not defined

In [2]:
# define size of the figure
#rcParams['figure.figsize'] = 10, 7

plot(xs, sin(xs), 'o-', label="sin(x)")
plot(xs, cos(xs), 'rx--', label="cos(x)")
xlabel("x")
ylabel("f(x)")
legend()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-fa0be6c4fa61> in <module>()
      2 #rcParams['figure.figsize'] = 10, 7
      3 
----> 4 plot(xs, sin(xs), 'o-', label="sin(x)")
      5 plot(xs, cos(xs), 'rx--', label="cos(x)")
      6 xlabel("x")

NameError: name 'plot' is not defined
  • plot to plot numpy arrays.
  • Have a look at the matplotlib gallery to see how to style the plots.

Subplots


In [ ]:
subplot(221)
plot(xs, sin(xs))
subplot(222)
plot(xs, cos(xs))
subplot(223)
plot(xs, exp(xs))
subplot(224)
plot(xs, log(xs))
  • Subplots can be created.
  • The number gives the plot grid and the number of the active subplot.

Log and Linear Scales


In [ ]:
xs = linspace(1.0, 10.0, 100)
subplot(221)
plot(xs)
plot(xs**2)
plot(0.001*exp(xs))

subplot(222)
loglog(xs)
loglog(xs**2)
loglog(exp(xs))

subplot(223)
semilogy(xs)
semilogy(xs**2)
semilogy(0.001*exp(xs))

subplot(224)
semilogx(xs)
semilogx(xs**2)
semilogx(0.001*exp(xs))

In [ ]:


In [ ]:


In [ ]: