Newer
Older
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
def __init__(self, n_bins, a, b):
"""Crée un histograme de n_bins réparties uniformément entre a et b"""
self._data = [0 for i in range(n_bins)]
self._n_bins = n_bins
self._a = a
self._b = b
def fill(self, x):
"""Remplis la bin correspondant à la valeur 'x'"""
bin_number = int((x-self._a)*self._n_bins/(self._b-self._a))
if bin_number >= 0 and bin_number < self._n_bins:
self._data[bin_number] += 1
return bin_number
else:
print("Warning, {} out of bounds.".format(x))
return -1
def get_maximum(self):
"""Retourne le maximum de l'histogramme"""
maximum = self._data[0]
for x in self._data[1:]:
if x > maximum:
maximum = x
return maximum
def draw(self):
"""Affiche l'histogramme en console"""
y_max = self.get_maximum()
scale = 1.
if y_max > 50:
scale = 50*1./y_max
y_max = 50
# vertical axis : 50 pxl
y = y_max + 1
while y > 0:
to_print = ""
if (y == y_max+1):
to_print = "y ^"
else:
to_print = " |"
for x in range(self._n_bins):
if self._data[x] >= y/scale:
to_print += "#"
else:
to_print += " "
print (to_print)
y-=1
print (" -"+"+"+(self._n_bins-1)*"-"+">")
print (" |"+(self._n_bins-1)*" "+"x")
if scale != 1:
print ("y-axis scale : 1 # = {}".format(1./scale))
def drawT(self):
"""Affiche l'histogramme en console verticalement (plus facile) """
# Scale my data (bin content) to a fixed width #
y_max = self.get_maximum()
width = 60
scale = width/y_max
scaled_data = [b*scale for b in self._data]
# Draw y axis #
print ('-' * width + '> y')
# Draw bin by bin #
for ibin,bin_content in enumerate(scaled_data):
line = f'{ibin:3d}|'
for y in range(width):
if bin_content>= y:
line += "#"
else:
line += " "
# Show the unscaled bin content on the right #
line += f'\t{self._data[ibin]:6.0f}'
print (line)
# Draw end of x axis #
print (' v')
print (' x')
# Print scaling of histogram #
print (f'Scaling : `#` = {scale:.5f}')
h.fill(0.5)
h.fill(0.5)
h.fill(0.5)
h.fill(0.1)
h.fill(2)
h.fill(-1)