#! /usr/bin/env python ## a simple program to read in a data file and make a histogram import math import string fname = 'hist_input.dat' ## give the parameters for histogramming histogram_min = -1.5 histogram_max = 1.5 n_histogram_points = 100 ## a list of separators for the histogram bins histogram_bounds = [histogram_min] for i in range(n_histogram_points): histogram_bounds.append(histogram_min + (i+1)*(histogram_max - histogram_min)/n_histogram_points) #print i, histogram_bounds[i] #print histogram_bounds ## now we can create the actual list of counts hist_counts = [0]*(len(histogram_bounds)) ## now read in the lines one by one and parse the data f = open(fname, 'r') all_lines = f.readlines() for line in all_lines: words = line.split() fwords = map(string.atof, words) ## loop over histogram_bounds to find the correct bin for i in range(len(histogram_bounds) - 1): ## now we look at fwords[1] because column 1 is what we are binning if fwords[1] >= histogram_bounds[i] and fwords[1] < histogram_bounds[i+1]: #print i, fwords[1] hist_counts[i] = hist_counts[i] + 1 ## now that we are done with binning we print bin numbers and counts ## first print some metadata print '## histogram from file', fname print '## histogram_min:', histogram_min print '## histogram_max:', histogram_max print '## n_histogram_points:', n_histogram_points ## now print the actual histogram counts ## note that you can plot the output with something like: ## python make-hists.py > hists.out ## $ gnuplot ## gnuplot> plot 'hist.out' using 1:2 with phist for i in range(len(hist_counts)): print histogram_bounds[i], ' ', hist_counts[i]