Created
July 12, 2017 10:47
-
-
Save terrycojones/217561fba13d415832048581901919e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 python | |
| from __future__ import print_function, division | |
| import sys | |
| import argparse | |
| import matplotlib.pyplot as plt | |
| parser = argparse.ArgumentParser( | |
| formatter_class=argparse.ArgumentDefaultsHelpFormatter, | |
| description='Examine stdin for numbers and plot them in a histogram.') | |
| parser.add_argument( | |
| '--bins', default=10, type=int, | |
| help='The number of bins in the histogram.') | |
| parser.add_argument( | |
| '--save', help='A file name to save a PNG image to.') | |
| parser.add_argument( | |
| '--noShow', dest='show', default=True, action='store_false', | |
| help='If given, do not automatically show the histogram image.') | |
| parser.add_argument( | |
| '--x', default='Count', help='X axis title.') | |
| parser.add_argument( | |
| '--y', default='Frequency', help='Y axis title.') | |
| parser.add_argument( | |
| '--title', default='Histogram', help='Histogram title.') | |
| args = parser.parse_args() | |
| if not (args.save or args.show): | |
| print('%s: You are neither showing nor saving the histogram... ' | |
| 'nothing to do!' % sys.argv[0], file=sys.stderr) | |
| sys.exit(1) | |
| x = [] | |
| append = x.append | |
| # Save anything on stdin that looks like a number. | |
| for line in sys.stdin: | |
| for value in line.split(): | |
| try: | |
| append(int(value)) | |
| except ValueError: | |
| try: | |
| append(float(value)) | |
| except ValueError: | |
| pass | |
| fig, ax = plt.subplots() | |
| ax.hist(x, bins=args.bins) | |
| ax.set_xlabel(args.x) | |
| ax.set_ylabel(args.y) | |
| ax.set_title(args.title) | |
| if args.save: | |
| fig.savefig(args.save) | |
| if args.show: | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment