Monthly Archives: December 2013
Lately, while working on my MBA thesis, there have been many times when I’ve been working in Python and wanted to plot a quick histogram of a distribution. The whole process has been far too time consuming. Either I need to remember how to write my list out as a CSV file so I can plot it in Excel/Tableau/SPSS (none of which, IMHO, has a particularly intuitive mechanism for drawing histograms) or I need to be able plot the histogram directly from Python. The later would be fine, except that I’m usually in an SSH shell and I always have trouble with X servers on my Windows 8 laptop.
So, anyway, I wrote this little function and it works quite nice for plotting histograms in pure text mode. It should be fine with any terminal client of the last 50 years. It isn’t particularly sophisticated, but it works for me and may work for you too.
def crappyhist(a, bins): '''Draws a crappy text-mode histogram of an array''' import numpy as np import string from math import log10 h,b = np.histogram(a, bins) for i in range (0, bins-1): print string.rjust(`b[i]`, 7)[:int(log10( np.amax(b)))+5], '| ', '#'*int(70*h[i-1]/np.amax(h)) print string.rjust(`b[bins]`, 7)[:int(log10(np.amax(b)))+5]