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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With