In [ ]:
%matplotlib inline
The argument after the ipython magic is called the backend
for plotting. There are several available, also for creating their own zoomable windows. But we also can zoom within the notebook, see below.
In [ ]:
import numpy as np
import matplotlib.pyplot as pl # import this for plotting routines
In [83]:
a = 9.8 # Acceleration m s^{-2}
count = 101 # Number of numbers
timeArray = np.linspace(0, 10, count) # Create an array of 101 times between 0 and 10 (inclusive)
distArray = 0.5 * a * timeArray**2 # Create an array of distances calculate from the times
In [84]:
print(timeArray)
print
print(distArray)
In [85]:
pl.scatter(timeArray, distArray, color = 'k')
Out[85]:
In [86]:
pl.scatter(timeArray, distArray, color = 'k')
pl.xlim(4, 6)
pl.ylim(50, 200)
pl.xlabel('time (s)')
pl.ylabel('distance (m)')
Out[86]:
In [87]:
%matplotlib notebook
In [88]:
pl.scatter(timeArray, distArray, color = 'k')
pl.xlim(4, 6)
pl.ylim(50, 200)
pl.xlabel('time (s)')
pl.ylabel('distance (m)')
Out[88]:
In [89]:
%matplotlib inline
In [94]:
pl.plot(timeArray, distArray, color='b', ls='-')
pl.xlabel('time (s)') # xlabel is the abscissa
pl.ylabel('distance (m)') # ylabel is the ordinate
Out[94]:
To save the figure, use savefig('filename') and the .pdf, or .eps, or .png, or ... extension (which Python interprets for you!):
In [96]:
pl.xlabel('time1 (s)')
pl.plot(timeArray, distArray, color='b', ls='-')
pl.ylabel('distance (m)')
pl.title('Position vs. Time')
pl.savefig('position_v_time.pdf') # In the same cell as pl.plot
pl.savefig('position_v_time.eps')
pl.savefig('position_v_time.png')
In [98]:
ls position_v_time*
Three topics today:
In [99]:
yArray = np.linspace(0, 5, 6) # take care of differences of interval determination here!
zArray = yArray[1:4]
print(yArray, zArray)
# Q. What will y and z contain?
In [100]:
yArray[3] = 10
In [101]:
print(yArray, zArray)
In [ ]:
zArray is not a copy of yArray, it is a slice of yArray!
AND:
All arrays generated by basic slicing are always views of the original array.
In other words, the variable zArray is a reference to three elements within yArray, elements 1, 2, and 3.
If this is not the desired behavior, copy arrays:
In [102]:
yArray = np.linspace(0, 5, 6)
zArray = yArray.copy()
print(yArray, zArray)
In [104]:
zArray = yArray.copy()[1:4] # you only `catch` the slice into new variable, rest of copy NOT
print(yArray, zArray)
In [105]:
yArray[3] = 10
print(yArray, zArray)
"copy" is an attribute of every numpy array, as are "shape", "size", "min", "max", etc.
If we want an array with the same "shape" as another array, we've seen that we can copy an array with:
In [106]:
xArray = np.array([1, 2, 3])
aArray = xArray.copy()
aArray
Out[106]:
then fill the array with the appropriate values.
However, we could also use numpy.zeros with the attributes xArray.shape and xArray.dtype:
In [108]:
print(xArray.shape) # this is a 1D vector
print(xArray.ndim)
xArray
Out[108]:
In [109]:
xArray.shape = (3,1)
In [110]:
print(xArray.shape) # Now it's a 3x1 2D matrix!
print(xArray.ndim)
xArray
Out[110]:
In [112]:
xArray.shape = (3,)
In [113]:
aArray = np.zeros(xArray.shape, xArray.dtype)
print(aArray.shape)
aArray
Out[113]:
Which gives aArray the same "shape" and data type as xArray.
In [116]:
np.zeros((2,3,4))
Out[116]:
Alternatively we could do:
In [117]:
aArray = np.zeros_like(xArray)
In [119]:
np.zeros??
In [120]:
bArray = np.ones_like(xArray)
In [121]:
print(aArray, bArray)
Subarrays can be sliced too, with or without range:
In [123]:
# remember, we already imported numpy (as np)!
xArray = np.linspace(1, 10, 10)
xArray
Out[123]:
In [124]:
# Note the double brackets indicating a subarray
xArray[[1, 5, 6]] = -1
xArray
Out[124]:
In [125]:
# Using range instead:
xArray = np.linspace(1, 10, 10)
xArray[range(3, 10, 3)] = -1
xArray
Out[125]:
In [126]:
# Compare
xArray = np.linspace(1, 10, 10)
xArray[[3, 6, 9]] = -1
xArray
Out[126]:
When do I use that?
Complementary methods for dealing with missing or invalid data: numpy masked arrays
http://docs.scipy.org/doc/numpy/reference/maskedarray.html
(masked arrays are a bit harder to use, but offer more powerful features)
For example, return a slice of the array consisting of negative elements only:
In [127]:
xArray
Out[127]:
In [128]:
myArray = xArray < 0
myArray
Out[128]:
In [129]:
xArray[xArray < 0]
Out[129]:
This will replace the elements of a new xArray with values less than zero with the maximum of xArray:
In [130]:
xArray = np.arange(-5, 5)
xArray
Out[130]:
In [131]:
xArray[xArray < 0] = xArray.max()
xArray
Out[131]:
numpy has routines for doing boolean logic:
In [132]:
xArray = np.arange(-5, 5)
xArray
Out[132]:
In [133]:
np.logical_and(xArray > 0, xArray % 2 == 1)
# % is the modulus: x % 2 == 1 means the remainder of x/2 is 1
# Q. So, what should running this cell give us?
Out[133]:
In [134]:
np.logical_or(xArray == xArray.min(), xArray == xArray.max())
Out[134]:
In [135]:
np.logical_not(xArray == xArray.min())
Out[135]:
In [136]:
print(np.any(xArray > 10))
print(np.any(xArray < -2))
In [137]:
print(np.all(xArray > -10))
print(np.all(xArray > -2))
In [ ]: