Class 8b: Numpy & Matplotlib

MarcGouw 480 views 7 slides Apr 19, 2016
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

Class 1 for course: "Programming, data analysis & data visualization in python". A course for the Champlimaud Neuroscience Program.


Slide Content

numpy
&
matplotlib

Installation...
in the ubuntu software center install:
-python-numpy
-python-matplotlib
www.virtualbox.org

numpy and matplotlib: a quick plot!

---- plots.py ----
import numpy as np
import matplotlib.pyplot as plt

# let's create some data
x = np.arange(0, 10)
y = np.sin(x)

# and now let's plot it!
plt.plot(x, y)
plt.show()

numpy arrays


---- plots.py ----
import numpy as np

# creating an new empty 2-dimensional array/matrix:
a = np.zeros([3, 10])

# creating an new 2-dimensional matrix from array/lists:
a = np.array([[1,10,4],[3,9,2]])

# accessing array elements: indexes and slicing
print a.shape

# accessing array elements: indexes and slicing
print a[1,2] # row 1, element 2
print a[1] # row 1
print a[:,1] # column 1

print a[:,0:2] # remember that x:y selects x to y-1







A one-dimensional array:
a = np.array(list)

A two-dimensional array:
a = np.array(list-of-lists)

A two-dimensional array of zeros:
a = np.zeros([3, 10])

numpy arrays

---- plots.py ----
import numpy as np

# creating an new 2-dimensional matrix from array/lists:
a = np.array([[1,10,4],[3,9,2]])

# changing a single values:
a[1,1] = 50
print a

# changing an entire column:
column = a[:,0]
a[:,1] = column

# basic math:
b = a[:,0] * 5
c = b + 9

# numpy math:
b = np.log10(a)




A single entry [row:col]
a[x:y]

A single row [row:col]
a[x,:]

A single column [row:col]
a[:,y]
see the full list of numpy mathfunctions:
http://docs.scipy.org/doc/numpy/reference/routines.math.html

Heatmap!


Transpose an array:
data = data.transpose()

absolute value of matrix:
data = np.abs(data)

log10 of a matrix:
data = np.log10(data)
1.Read the matrix from the file (use np.loadtxt(“data.txt”))
2.Transpose the matrix
3.Apply a log10 transformation to all values
4.Copy row 11 to 15 and 18 (start counting at zero) (don’t forget n -1!)
5.Copy column 8 to columns 9 to 11 (start counting at zero) (don’t forget n -1!)
6.Transform all numbers to positive
7.Multiply all numbers between coordinates (2,3) and (18,7) by 20
8.Display the final result as a heatmap. It should be obvious if you got it right :P .
displaying a heatmap:
plt.pcolor(data)
plt.show()
selecting value, rows, columns...
data[x:y] (value)
data[x,:] (row)
data[:,y] (column)

making plots just a little prettier!

---- plots.py ----
import numpy as np
import matplotlib.pyplot as plt

# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)

# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()