Histogram without charting function

I am trying to create a simple text histogram using python, but without importing any build functions like matplot or gnuplot. I will import data from the csv file to create this histogram.

+8
python
source share
1 answer

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) 

What 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) 
+12
source share

All Articles