Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yangliu28
GitHub Repository: yangliu28/swarm_formation_sim
Path: blob/master/loop_reshape_test_power.py
104 views
1
# demo of how power function can increase the unipolarity of a random distribution
2
3
# pass the exponent of the power function in the command line
4
# ex: 'python loop_reshape_test_power.py 1.3'
5
6
import matplotlib.pyplot as plt
7
import numpy as np
8
import random, time, os, sys
9
10
# get the exponent from passing parameter
11
exponent = float(sys.argv[1])
12
13
N = 50 # number of columns in the distribution
14
15
x_data = np.array(range(N))
16
y_data = np.array([random.random() for i in range(N)])
17
y_data_sum = np.sum(y_data)
18
y_data = y_data/y_data_sum
19
20
fig = plt.figure()
21
ax = fig.add_subplot(111)
22
rects = ax.bar(x_data, y_data, align='center')
23
ax.set_xlim(-1, N)
24
ax.set_ylim(0.0, 1.0)
25
fig.canvas.draw()
26
fig.show()
27
28
while True:
29
sys.stdout.write('+')
30
sys.stdout.flush()
31
32
y_data = np.power(y_data, exponent) # the power function
33
y_data_sum = np.sum(y_data)
34
y_data = y_data/y_data_sum
35
36
for i in range(N):
37
rects[i].set_height(y_data[i])
38
fig.canvas.draw()
39
fig.show()
40
41
time.sleep(0.3)
42
43
44
45