-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
71 lines (58 loc) · 2.09 KB
/
plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
import random
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import make_interp_spline
from numba import jit
#width of the plot or height of the galton board
width=30
#total number of beads to release at the top
number_of_beads=40000
#=============================================================================================
#array to hold the data where the bead lands
data=[0]*(width+1)
#setting x axis range accordingly if width is even or odd
lrange=-int(width/2)
if width%2: #for even
urange=-lrange+2
else:
urange=-lrange+1
#array for plotting
x_axis= [i for i in range(lrange,urange)]
#array for spline plotting
x_spline = xnew = np.linspace(min(x_axis), max(x_axis), 300)
#use numba for seedup
@jit(nopython=True)
def update():
coordinate=0
for j_th_level in range(width):
if random.randint(0,1): #if random output between 0 and 1 gives 1(True)
coordinate+=1 #increase the coordinate (bead goes to the right)
return coordinate
#main simulation loop
for i_th_bead in range(1,number_of_beads+1):
plt.clf() #clear the plot
coordinate=update()
"""
NOTE:Bead never travels left ,This does not affect result,
but shifts the data, this is taken care of while plotting
"""
#Count is incremented in the corresponding place where the bead "lands"
data[coordinate]+=1
print('bead number =',i_th_bead,end='\r')
#update plot in intervals of 250 beads
if i_th_bead%250==0:
Spline = make_interp_spline(x_axis, data, k=3)
y_spline = Spline(x_spline)
plt.title("Number of 'Virtual Beads'={}".format(i_th_bead))
plt.bar(x_axis,data,color='black')
#Hide xtics
plt.xticks([])
plt.plot(x_spline,y_spline,color='red')
plt.pause(0.00001)
# plt.savefig("frames/{}.png".format(int(i_th_bead)),dpi=200)
## uncomment line above to save the individual frames to create animation if needed
print('\n complete,close the window to exit')
plt.bar(x_axis,data,color='black')
plt.plot(x_spline,y_spline,color='red')
plt.show()