Skip to content

Instantly share code, notes, and snippets.

@stfuchs
Last active October 23, 2019 18:10
Show Gist options
  • Save stfuchs/a43b6c90e481b81b0739cccfda971243 to your computer and use it in GitHub Desktop.
Save stfuchs/a43b6c90e481b81b0739cccfda971243 to your computer and use it in GitHub Desktop.
import pandas as pd
def histogram(values, bins=100, xmin=None, xmax=None, xstr=lambda x: '%s' % x, linewidth=120, symbol='B0'):
sym = {
'B1': u'\u2591', # Light shade
'B2': u'\u2592', # Medium shade
'B3': u'\u2593', # Dark shade
'B4': u'\u2588', # Full Block
'B0': u'\u25a0'} # Black square
x = pd.Series(values)
xmin = xmin or x.min()
xmax = xmax or x.max()
counts = x[(x >= xmin) & (x <= xmax)].value_counts(sort=False, bins=bins)
strings = [(xstr(bucket.mid), count) for bucket, count in counts.iteritems()]
max_string = max(map(lambda x: len(x[0]), strings))
scale = (linewidth - 11. - max_string) / counts.max()
print("# NumSamples = %d; Min = %s; Max = %s" % (len(values), xstr(xmin), xstr(xmax)))
for name, count in strings:
print('%s [%6d]: %s' % (name.ljust(max_string), count, sym.get(symbol, symbol) * int(count * scale)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment