forked from grantjenks/python-sortedcontainers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_lengths_histogram_delitem.py
More file actions
67 lines (50 loc) · 1.78 KB
/
Copy pathplot_lengths_histogram_delitem.py
File metadata and controls
67 lines (50 loc) · 1.78 KB
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
"""Plot histogram of SortedList sublist lengths while items are deleted.
"""
from __future__ import division, print_function
import argparse
import itertools as it
import numpy as np
import scipy
import scipy.stats
import sortedcontainers as sc
from matplotlib import pyplot as plt
from matplotlib import animation
import random
def background():
"Plot the background of each animated frame."
hist_line.set_data([], [])
norm_line.set_data([], [])
return hist_line, norm_line
def frame(num):
"Draw frame."
for value in xrange(LOAD):
del values[random.randrange(len(values))]
data = np.array([len(sublist) for sublist in values._lists])
hist, bins = np.histogram(data, bins=BINS) # , normed=True)
hist_line.set_data(bins[:-1], hist)
return hist_line, norm_line
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--load', type=float, default=1e3)
parser.add_argument('--show', action='store_true')
parser.add_argument('--filename', default='sublist-lengths-delitem.mp4')
args = parser.parse_args()
random.seed(args.seed)
LOAD = int(args.load)
BINS = np.array(range(LOAD // 2, LOAD * 2 + 1, LOAD // 20))
values = sc.SortedList(xrange(LOAD * LOAD), load=LOAD)
fig = plt.figure()
bounds = (LOAD // 2 - 5 * LOAD // 100, LOAD * 2 - 5 * LOAD // 100)
ax = plt.axes(xlim=bounds, ylim=(0, 1000))
hist_line, = ax.plot([], [])
norm_line, = ax.plot([], [])
animator = animation.FuncAnimation(
fig, frame, init_func=background, frames=1000, interval=10, blit=True,
)
if args.show:
plt.show()
else:
animator.save(
args.filename, fps=20, extra_args=['-vcodec', 'libx264']
)