from Controller import isNumeric, histDict import Gnuplot, Gnuplot.funcutils, Numeric xTickNameLength = 3 def mycmp(a, b): if isNumeric(a): if isNumeric(b): return cmp(float(a), float(b)) else: return cmp(float(a), -1) else: if isNumeric(b): return cmp(-1, float(b)) else: return cmp(a, b) return cmp(a, b) def drawHistogram(L, title, quantize, step): if quantize: drawQuantizedHist(L, title, step) return 0 g = Gnuplot.Gnuplot(debug=1) g.title(title) g('set boxwidth 0.5 relative') g('set style fill solid 1.0 border -1') g('set datafile missing \'-\'') g('set style data boxes') g('set boxwidth 0.6') g('set ylabel "Frequency"') a = histDict(L) mykeys = a.keys() mykeys.sort(mycmp) spam = 'set xtics(' L2 = [] for i in range(0, len(mykeys)): spam = spam + "\"" + repr(mykeys[i])[:(xTickNameLength * 2)] + "\" " + str(i) + ", " L2.append(a[mykeys[i]]) spam = spam[:-2] + ")" g(spam) spam = 'set yrange [ 0 : ' + str(max(a.values())) + ' ] noreverse nowriteback' g(spam) g.plot(L2) def mymin(a, b): if (a < b): return a return b def mymax(a, b): if (a > b): return a return b def drawQuantizedHist(L, title, step): g = Gnuplot.Gnuplot(debug=1) g.title(title) g('set boxwidth 0.5 relative') g('set style fill solid 1.0 border -1') g('set datafile missing \'-\'') g('set style data boxes') g('set boxwidth 0.6') g('set ylabel "Frequency"') a = histDict(filter(lambda x: not(isNumeric(x)), L)) # Get non-numeric entries out. mykeys = a.keys() mykeys.sort(mycmp) spam = 'set xtics(' L2 = [] i2 = len(mykeys) for i in range(0, len(mykeys)): spam = spam + "\"" + repr(mykeys[i])[:(xTickNameLength * 2)] + "\" " + str(i) + ", " L2.append(a[mykeys[i]]) a = histDict(map( lambda x: (int(x / step) * step), filter(isNumeric, L) )) mykeys = a.keys() mykeys.sort(mycmp) for i in range(i2, len(mykeys)): spam = spam + "\"" + repr(int(mykeys[i]) - step)[:xTickNameLength] + "-" + repr(int(mykeys[i]))[:xTickNameLength] + "\" " + str(i) + ", " L2.append(a[mykeys[i]]) spam = spam[:-2] + ")" g(spam) spam = 'set yrange [ 0 : ' + str(max(a.values())) + ' ] noreverse nowriteback' g(spam) g.plot(L2)