Week 5 - Numpy & Matplotlib

Today's Agenda

  • Numpy
  • Matplotlib

Numpy - Numerical Python

From their website (http://www.numpy.org/):

NumPy is the fundamental package for scientific computing with Python.

  • a powerful N-dimensional array object
  • sophisticated (broadcasting) functions
  • tools for integrating C/C++ and Fortran code
  • useful linear algebra, Fourier transform, and random number capabilities

You can import "numpy" as

In [ ]:
import numpy as np

Numpy arrays

In standard Python, data is stored as lists, and multidimensional data as 'lists of lists'. In numpy, however, we can now work with arrays. To get these arrays, we can use np.asarray to convert a list into an array. Below we take a quick look at how a list behaves differently from an array.

In [ ]:
x=range(1,11,1)
x_array=np.asarray(x)
print x, x*2
print x_array, x_array*2

If we want to set up an array in numpy, we can use range to make a list and then convert it to an array, but we can also just create an array directly in numpy. np.arange will do this with integers, and np.linspace will do this with floats, and allows for non-integer steps.

In [ ]:
print np.arange(10)
print np.linspace(1,10,10)

Last week we had to use a function or a loop to carry out math on a list. However with numpy we can do this a lot simpler by making sure we're working with an array, and carrying out the mathematical operations on that array.

In [ ]:
x=np.arange(10)
print x
print x**2

In numpy, we also have more options for quickly (and without much code) examining the contents of an array. One of the most helpful tools for this is np.where. np.where uses a conditional statement on the array and returns an array that contains indices of all the values that were true for the conditional statement. We can then call the original array and use the new array to get all the values that were true for the conditional statement.

There are also functions like max and min that will give the max and min, respectively.

In [ ]:
x=np.linspace(0,100,201)
print x
x_5=np.where(x%5 == 0)
print x_5
print x[x_5]
print np.max(x)
print np.min(x)

Numpy also provides some tools for loading and saving data, loadtxt and savetxt. Here I'm using a function called transpose so that instead of each array being a row, they each get treated as a column.

When we load the information again, it's now a 2D array. We can select parts of those arrays just as we could for 1D arrays.

In [ ]:
x=np.linspace(0,100,501)
y=.1*x**2-5*x+3
np.savetxt('myfile.txt', np.transpose([x,y]))
data=np.loadtxt('myfile.txt')
print data
print data[:3,:]

Resources

Matplotlib

Matplotlib is a Python 2D plotting library which

  • produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms
  • Quick way to visualize data from Python
  • Main plotting utility in Python

From their website (http://matplotlib.org/):

Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shell, the jupyter notebook, web application servers, and four graphical user interface toolkits.

A great starting point to figuring out how to make a particular figure is to start from the Matplotlib gallery and look for what you want to make.

In [ ]:
import matplotlib.pyplot as plt
import numpy as np
data=np.loadtxt('myfile.txt')
#importing numpy and matplotlib and loading the data

The simplest figure is to simply make a plot. We can have multiple figures, but for now, just one. The plt.plot function will connect the points, but if we want a scatter plot, then plt.scatter will work.

In [ ]:
plt.figure(1)
plt.plot(data[:,0],data[:,1])
plt.show()

We can take that same figure and add on the needed labels and titles.

In [ ]:
plt.figure(1)
plt.plot(data[:,0],data[:,1])
plt.title('y=0.2x^2-5x+3')
plt.xlabel('x value')
plt.ylabel('y value')
plt.show()

There's a large number of options available for plotting, so try using the initial code below, combined with the information here to try out a few of the following things: changing the line width, changing the line color

In [ ]:
plt.figure(1)
plt.plot(data[:,0],data[:,1])
plt.title('y=0.2x^2-5x+3')
plt.xlabel('x value')
plt.ylabel('y value')
plt.show()