Created
January 14, 2016 19:30
-
-
Save simonrad/d3279abadf7c3e6e762e to your computer and use it in GitHub Desktop.
Python script to plot a histogram of a set of numbers.
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 | |
| ''' | |
| This script reads a set of numbers and plots a histogram. | |
| ''' | |
| import sys | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| if len(sys.argv) != 2: | |
| print 'Usage: python {} path/to/data/file'.format(sys.argv[0]) | |
| print 'Or: pbpaste | python {} stdin'.format(sys.argv[0]) | |
| print ' (This way it will plot whatever you most recently copied.)' | |
| print '(The data should be a sequence of numbers separated by either "," or newlines.)' | |
| print 'Error: This program takes one argument. See above.' | |
| print 'Quitting.' | |
| sys.exit() | |
| filepath = sys.argv[1] | |
| if filepath == 'stdin': | |
| data_from_file = sys.stdin.read().strip() | |
| else: | |
| data_from_file = open(filepath).read().strip() | |
| def is_delim_by(delim): | |
| return len(data_from_file.split(delim)) > 3 | |
| if is_delim_by(',') and '\n' not in data_from_file: | |
| numbers = np.array([float(x) for x in data_from_file.split(',')]) | |
| elif is_delim_by('\n') and ',' not in data_from_file: | |
| numbers = np.array([float(x) for x in data_from_file.split('\n')]) | |
| else: | |
| print 'Error: Not sure how to parse the data.' | |
| print 'The data should be a sequence of numbers separated by either "," or newlines.' | |
| print 'Quitting.' | |
| sys.exit() | |
| p95 = np.percentile(numbers, 95) | |
| p05 = np.percentile(numbers, 5) | |
| plt.hist(numbers, bins=50, range=(p05, p95)) | |
| plt.title('Histogram') | |
| plt.xlabel('Value') | |
| plt.ylabel('Frequency') | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment