Last active
June 5, 2017 17:56
-
-
Save zaxtax/ca5103c9c20d7c71de457eca6de636ff to your computer and use it in GitHub Desktop.
Histogram from the command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import click | |
import numpy as np | |
import sys | |
class IntOrStr(click.ParamType): | |
name = 'int/str' | |
def convert(self, value, param, ctx): | |
try: | |
return int(value) | |
except ValueError: | |
return value | |
@click.command() | |
@click.option('--bins', | |
default="fd", | |
type=IntOrStr(), | |
help=""" | |
Number of bins to use, or | |
a binning strategy (fd, sturges, sqrt, etc.) | |
""") | |
@click.option('--width', default=50, help='Max width for histogram.') | |
def hist(bins, width): | |
data = np.fromiter(sys.stdin.readlines(), np.float) | |
h = np.histogram(data, bins=bins) | |
scale = max(h[0]) / width | |
for i in range(len(h[0])): | |
print("{0: 0.2f}".format(h[1][i]), "="*int(h[0][i] // scale)) | |
if __name__ == '__main__': | |
hist() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An example usage might look like