1

I want to make a vertical histogram using Python by printing an asterisk pattern. It's pretty easy to do same in horizontal but how to do it vertically?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Aditya Rakhecha
  • 265
  • 1
  • 3
  • 10
  • Does this relate to opencv or image-processing since they are tagged? Do you have code for the horizontal version at least? Have you tried anything for the vertical? I'm thinking a 2d array to store the output. – Zev Jun 12 '18 at 04:25

1 Answers1

2

You need to print out the data in rows, starting with the row for the maximum histogram value.

In each row, you will print out a '*' for each bucket where that bucket has a value greater than or equal to the value corresponding to the current row, and a space where the value is smaller.

Eg) Histogram for the sum of three 6-sided dice:

>>> r = list(x+y+z+3 for x in range(6) for y in range(6) for z in range(6))
>>> data = [r.count(v) for v in range(max(r)+1)]
>>> for y in range(max(data), 0, -3):
...   print(*('*' if v >= y else ' ' for v in data))
... 
                    * *              
                  * * * *            
                * * * * * *          
                * * * * * *          
              * * * * * * * *        
              * * * * * * * *       
            * * * * * * * * * *      
          * * * * * * * * * * * *    
        * * * * * * * * * * * * * *  
>>> 
AJNeufeld
  • 8,526
  • 1
  • 25
  • 44