Skip to content
Snippets Groups Projects
Commit 0d65030a authored by Andreas Kloeckner's avatar Andreas Kloeckner
Browse files

Add histogram creation.

parent 7216f9f3
No related branches found
No related tags found
No related merge requests found
......@@ -971,6 +971,49 @@ class Table:
def string_histogram(iterable, min_value=None, max_value=None, bin_count=20, width=75,
use_unicode=True):
if min_value is None or max_value is None:
iterable = list(iterable)
min_value = min(iterable)
max_value = max(iterable)
bin_width = (max_value - min_value)/bin_count
bins = [0 for i in range(bin_count)]
from math import floor, ceil
for value in iterable:
bin_nr = int(floor((value-min_value)/bin_width))
if 0 <= bin_nr < bin_count:
bins[bin_nr] += 1
else:
from warnings import warn
warn("string_histogram: out-of-bounds value ignored")
if use_unicode:
def format_bar(cnt):
scaled = cnt*width/max_count
full = int(floor(scaled))
eighths = int(ceil((scaled-full)*8))
if eighths:
return full*unichr(0x2588) + unichr(0x2588+(8-eighths))
else:
return full*unichr(0x2588)
else:
def format_bar(cnt):
return int(ceil(cnt*width/max_count))*"#"
max_count = max(bins)
total_count = sum(bins)
return "\n".join("%9g (%9d:%3.0f %%) : %s" % (
min_value+bin_nr*bin_width,
bin_value,
bin_value/total_count*100,
format_bar(bin_value))
for bin_nr, bin_value in enumerate(bins))
# command line interfaces -----------------------------------------------------
class CPyUserInterface(object):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment