1

I'm trying to create a histogram in python

My fuction is the next one:

        def hist_hor(diccionario):
            dict={}
            
            for e in diccionario:
                if e  in dict:
                    dict[e] += '*'
                else: 
                    dict[e]= '*'
            for i in sorted(dict):
        
                print(f'{i}: {dict[i]}')
    
    histograma_horizontal(d)

It is supposed to look like this:

a: *****
b: **********
c: ************
d: ***********
e: ***************
f: ********************
g: ***************
h: *********
i: *******
j: **

but with my function it looks like this:

a: *
b: *
c: *
d: *
e: *
f: *
g: *
h: *
i: *
j: *

Besides, anyone who knows to represent it in that way too ?:

          *        
          *        
          *        
          *        
        * * *      
        * * *      
        * * *      
    *   * * *      
    * * * * *      
  * * * * * *      
  * * * * * * *    
  * * * * * * *    
  * * * * * * * *  
  * * * * * * * *  
* * * * * * * * *  
* * * * * * * * *  
* * * * * * * * *  
* * * * * * * * * *
* * * * * * * * * *
2
  • What is diccionario? Please share it Commented Dec 18, 2021 at 18:46
  • This seems like a decent use for a defaultdict. Commented Dec 19, 2021 at 1:21

1 Answer 1

3

You can complete the first part without creating a new dictionary (remember to use end to prevent a new line):

NOTE - Tested in Ubuntu 20.04, using Python 3.8.

def hist_hor(diccionario):
    for key, value in diccionario.items():
        print(f"{key}: ", end="")
        for _ in range(value):
            print("*", end="")
        print()


diccionario = {"a": 5, "b": 10, "c": 12, "d": 11, "e": 15,
               "f": 20, "g": 15, "h": 9, "i": 7, "j": 2, }

hist_hor(diccionario)

Output:

a: *****
b: **********
c: ************
d: ***********
e: ***************
f: ********************
g: ***************
h: *********
i: *******
j: **

But if you need to create a new dictionary, use update instead of trying to manipulate indexes:

def hist_hor(diccionario):
    # Do not use dict as a variable name; it shadows a builtin type
    new_dicc = {}
    for key, value in diccionario.items():
        temp = ""
        for _ in range(value):
            temp += "*"
        new_dicc.update({key: temp})
    return new_dicc

d = hist_hor(diccionario)
for key, value in d.items():
    print(f"{key}: {value}")

The output will be the same.

For the second part, get the max value first and create a reverse loop. Iterate through the values, and if the value is greater than or equal to the loop index, print an asterisk:

def hist_hor(diccionario):
    m = max(diccionario.values())
    for i in range(m, 0, -1):
        for x in diccionario.values():
            if x >= i:
                print("* ", end="")
            else:
                print("  ", end="")
        print()

Output:

          *         
          *         
          *         
          *         
          *         
        * * *       
        * * *       
        * * *       
    *   * * *       
    * * * * *       
  * * * * * *       
  * * * * * * *     
  * * * * * * *     
  * * * * * * * *   
  * * * * * * * *   
* * * * * * * * *   
* * * * * * * * *   
* * * * * * * * *   
* * * * * * * * * * 
* * * * * * * * * * 
Sign up to request clarification or add additional context in comments.

2 Comments

There are a few ways. You can use operator.itemgetter and collections.OrderedDict, or you can flip the values and the keys: new_dicc = dict(sorted((value, key) for (key, value) in new_dicc.items())) The easiest way is to use sorted with a lambda function: new_dicc = dict(sorted(new_dicc.items(), key=lambda item: item[1]))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.