Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram without plotting function

Tags:

python

I am trying to create a simple text based histogram using python but without importing any plotting functions such as matplot or gnuplot. I will be importing data from a csv file to create that histogram.

like image 340
bluelantern Avatar asked May 26 '12 04:05

bluelantern


1 Answers

How about something like this

import random

def plot(data):
    """
    Histogram data to stdout
    """
    largest = max(data)
    scale = 50. / largest
    for i, datum in enumerate(data):
        bar = "*" * int(datum * scale)
        print "%2d: %s (%d)" % (i, bar, datum)

data = [ random.randrange(100) for _ in range(20) ]
plot(data)

Which prints something like this

 0: ************************ (48)
 1: ************************************************** (99)
 2: *********************************** (71)
 3: ******************************************** (88)
 4: ********** (21)
 5: ************************************* (74)
 6: ********************************* (67)
 7: *************************** (54)
 8: ************************************************* (98)
 9: *************** (31)
10: *********** (23)
11: ****************************** (61)
12: ********** (20)
13: **************** (33)
14: **** (8)
15: **************************** (57)
16: ***************************** (59)
17:  (1)
18: ************************ (48)
19: *** (6)
like image 174
Nick Craig-Wood Avatar answered Nov 06 '22 09:11

Nick Craig-Wood