Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

print the distribution in python using dashes

Here’s my code right now. So I would like to visualize a normal distribution graph in python only using these dashes (no import)
I am not sure how I should convert the numbers of frequency into dashes. Any ideas? Or any suggestions on the design of my existing codes?

normal_list = []  # normally distributed random numbers
for i in range(200000):  # generate 200,000 numbers
    normal_list.append(round(random.normalvariate(10, 7)))  # with mu=10, sigma=7
normal_freq = {}
for i in range(len(normal_freq)):
    if normal_list[i] not in normal_freq:
        normal_freq[normal_list[i]] = 1
    normal_freq[normal_list[i]] += 1

# These codes are for scaling frequency purposes
# avoid too many freq numbers
norm_max = max(normal_freq.values())
norm_min = min(normal_freq.values())
for val, freq in normal_freq.items():
    normal_freq[val] = round((freq - norm_max) * 99 / (norm_max - norm_min) + 100)

for val, freq in normal_freq.items():
    print(val, ': ', freq)
Here is the desired sample output:
1: ---​
2: --------​
4: -----------​
7: -------​
8: --​
11: -

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your print statement should be something like print("%d: %s" % (val, '-' * freq) ).

The magic is in the '-' * freq expression; what this does is print out freq dashes (e.g. if freq == 3, then you get ---.

Edit: See BeRT2me’s comment below if what I wrote is a little unreadable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading