import numpy as np
from matplotlib import pyplot as plt
#Create an array
x = np.array([3,2,1,5,9,3,2,3])
print(x)
[3 2 1 5 9 3 2 3]
#Index by value, select only values greater than 4
#If you just print x>4 you get a boolean saying if the condition is met by the value in the array
print(x>4)
#If you apply the boolean to the original array, the output is the values that meet the condition
print(x[x>4])
[False False False True True False False False] [5 9]
#This is important for sampling data, like say you want to determine the
# average pressure on days that it rains in Miami, FL based on a weather station
#Create a random set of data
time = np.arange(0,300)*np.pi/4.
rain = np.abs(np.random.normal(0,1,size=300)*np.cos(.3*time))
pressure = 1000.-np.random.randint(0,15,size=300)*np.sin(.1*time)
#What is the mean rainfall?
print(np.mean(rain))
#what is the mean pressure?
print(np.mean(pressure))
#You can use indexing from one variable with another as long as they are the same shape
#what is the mean pressure when it rains more than .5?
print(np.mean(pressure[rain>0.5]))
#Or you can find what what the average rainfall for given pressure?
print(np.mean(rain[pressure>1005.]))
#If you want to use multiple conditions, you use an & and must use ()
#You want to find the average rainfall for a given pressure only during the first 100 days
print(np.mean(rain[(pressure>1005.) & (time<100)]))
0.496563027368 999.473221718 999.393359529 0.447668462557 0.319246011295
ax1 = plt.subplot(111)
ax2 = ax1.twinx()
ax1.plot(time,rain,c='r',lw=3,alpha=.9)
ax2.plot(time,pressure,c='b',lw=3,alpha=.9)
ax1.set_xlabel('Time')
ax1.set_ylabel('Rain')
ax2.set_ylabel('Pressure')
plt.show()