'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 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
