In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as pl
Figures
pl.subplots() returns the figure and an array of axes, in the shape you want to plot:
In [4]:
img1 = np.loadtxt('../Tutorials/starfield_exp1.txt')
img2 = np.loadtxt('../Tutorials/starfield_exp2.txt')
# Side-by-side plots!
# Subplot values: (number of rows, number of columns, plot number)
fig, (ax1, ax2) = pl.subplots(ncols=2, figsize=(12,6))
# Show each image
ax1.imshow(img1, origin='lower')
ax2.imshow(img2, origin='lower')
# Get the coordinates of the middle of the image
xMid = img1.shape[0] / 2
yMid = img1.shape[1] / 2
# Overplot circle around starting point.
ax1.scatter(xMid, yMid, color='k', marker='o', s=100, facecolors='none') # Hollow points of size = 100
ax2.scatter(xMid, yMid, color='k', marker='o', s=100, facecolors='none')
# Diff the images
img3 = img2 - img1
# Get
endpt = np.argwhere(img3 == img3.min())
ax2.scatter(endpt[0][0], endpt[0][1], color='r', marker='o', s=100, facecolors='none')
Out[4]:
In [6]:
import random
print(random.random())
print(random.random())
print(random.random())
In [7]:
random.seed(2)
print(random.random())
random.seed(2)
print(random.random())
random.seed(2)
print(random.random())
In [8]:
print(100 * random.random())
print(100 * random.random())
print(100 * random.random())
In [9]:
random.uniform(10, 20)
Out[9]:
In [12]:
import random
# Pick a seed:
random.seed(5)
# Let's have 1000 samples:
N = 1000
x = range(N)
# List comprehension to generate random numbers in y between 0 and 100:
y = [random.uniform(0, 100) for _ in x]
In [13]:
pl.scatter(x, y)
Out[13]:
In [14]:
random.seed(5)
N = 1000 # 1000 samples
x = range(N)
y = [random.uniform(0, 100) for _ in x]
# Now, plot a histogram of the data
#
# hist() returns:
# the number of values in each bin <array>
# the bin x-coord values <array>
# the patches <array>
#
# A Patch describes a bin geometrically
nArray, binArray, patchArray = pl.hist(y, bins=50, facecolor='g')
# Label the plot and add a grid for visualization:
pl.xlabel('Random Number')
pl.ylabel('Number of Occurances')
pl.grid(True)
# Print each returned value from hist()
print(nArray)
print()
print(binArray)
print()
print(patchArray)
print()
print(patchArray[0])
In [15]:
# Hint: What is the average number of hits
# in each bin and how many bins are there?
print(len(nArray))
In [16]:
# Same as above
random.seed(5)
N = 1000
x = range(N)
y = [random.uniform(0, 100) for _ in x]
# Now, note "normed = True" to normalize to 1:
nArray, binArray, patchArray = pl.hist(y, bins=50, normed=True, facecolor='g')
# Then plot
pl.xlabel('Random Number')
pl.ylabel('Probability of Occurance')
pl.grid(True)
nArray
Out[16]:
In [17]:
import numpy as np
randUniArray = np.random.uniform(0, 10, size=10000) # Range from 0 to 10
In [18]:
sum(randUniArray)
Out[18]:
In [20]:
import numpy as np
mu = 100 # Average of 100
sigma = 20 # Standard deviation of 20
# randn = normal distribution (unity normalized by default)
randNormArray = mu + sigma * np.random.randn(10000)
# Q. What data type should x be?
type(randNormArray)
Out[20]:
In [21]:
# Histogram of the data:
#nArray, binArray, patchArray = pl.hist(randNormArray, 100, normed=True, facecolor='g')
pl.hist(randNormArray, 100, normed=True, facecolor='g')
pl.xlabel('x', size='large')
pl.ylabel('Probability of x', size='large')
pl.title('Normal Distribution')
pl.text(40, 0.015, r'$\mu=100,\ \sigma=20$', size='x-large')
pl.grid(True)
In [22]:
mu = 0
sigma = 20
randNormArray = mu + sigma * np.random.randn(1000000)
nArray, binArray, patchArray = pl.hist(randNormArray, 100, normed=1, facecolor='r')
pl.xlabel('x', size='large')
pl.ylabel('Probability of x', size='large')
pl.title('Normal Distribution')
pl.text(40, .015, r'$\mu=0,\ \sigma=20$', size='x-large')
pl.grid(True)
In [24]:
mu = 100
sigma = 20
# Method #1
randNormArray1 = mu + sigma * np.random.randn(10000)
# Method #2
randNormArray2 = np.random.normal(size=10000, loc=mu, scale=sigma)
# Compare
_, _, _ = pl.hist(randNormArray1, bins=100, color='b')
_, _, _ = pl.hist(randNormArray2, bins=100, color='g')
In [ ]: