How can I create a histogram of the values and their count without using the hist() function?

Viewed 84

I cannot figure out a way to print the histogram of the values in format of range. I wanted to write a code to output the items and counts(multiply 'x') ). if a item does not exist, output will '' Here is what I wrote so far:

item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
max=15
min=0
def histogram( count, item ):
    i=0
    if(max-min<=5):
        for n in count:
            output = ''
            times = n
            while( times > 0 ):
              output += '*'
              times = times - 1
            #print(item[i],output)
            print(f'{item[i]:<10}',output)
            i+=1  
    elif(max-min>5):
        #print the histogram in range of 5s
histogram( count, item )

Output(if the max-min<5)-this won't happen in above program since max-min is larger than 5 but I wanted to show u how it will look like if we assume max-min was less than 5:

15         *
14         *
13         *
12         *
11         ***
10
9
8
7         *
6
5
4         *
3
2
1
0          *

Output for (max-min>5)

10-15  *******
5-10   *
0-5    *
2 Answers

range takes step like range(start, end, step). You can use step to tell to go a step of 5 like 15-10, 10-5, 5-0

item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
max_=15
min_=0
def histogram( count, item ):
    i=0
    if(max_-min_<=5):
        for n in count:
            output = ''
            times = n
            while( times > 0 ):
                output += '*'
                times = times - 1
            #print(item[i],output)
            print(f'{item[i]:<10}',output)
            i+=1  
    elif(max_-min_>5):
        for i in range(0, len(item)-5, 5):
            label = item[i] + "-" + item[i+5]
            print(f'{ label:<10}','*' * sum(count[i: i+5]))
        
histogram(count, item )
15-10      *******
10-5       *
5-0        *

Cleaned your code up a bit. '*'*x will print '*' x times.

This also handles the case where the number of items is not a multiple of 5.

item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
maxi=15
mini=0
def histogram( count, item ):
    if(maxi-mini<=5):
        for i, c in zip(item, count):
            output = '*'*c
            print(f'{i:<10}',output)
    elif(maxi-mini>5):
        for index in range(0, len(item), 5):
            #print the histogram in range of 5s
            end_index = min(index+4, len(item)-1) # this is to handle if number of items is not divisible by 5
            range_str = f'{item[end_index]}-{item[index]}'
            output = '*'*sum(count[index:end_index+1])
            print(range_str, output)
histogram( count, item )

This prints:

11-15 *******
6-10 *
1-5 *
0-0 *

It would work better if we invert the item and count lists first with

item = list(reversed(item))
count = list(reversed(count))

and tweaking the function a bit:

item=['15', '14', '13', '12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0']
count=[1, 1, 1, 1, 3, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1]
maxi=15
mini=0
def histogram( count, item ):
    if(maxi-mini<=5):
        for i, c in zip(item, count):
            output = '*'*c
            print(f'{i:<10}',output)
    elif(maxi-mini>5):
        for index in range(0, len(item), 5):
            #print the histogram in range of 5s
            end_index = min(index+4, len(item)-1) # this is to handle if number of items is not divisible by 5
            range_str = f'{item[index]}-{item[end_index]}'
            output = '*'*sum(count[index:end_index+1])
            print(range_str, output)

This prints

0-4 **
5-9 *
10-14 ******
15-15 *
Related