简体   繁体   中英

How can i use histogram graph on this probability code in python

Can somebody tell me how can i create histogram graph from my this output.

output of this code.

{89: 0.015, 51: 0.02, 71: 0.005, 70: 0.014, 8: 0.006, 12: 0.004, 53: 0.009, 2: 0.012, 58: 0.016, 59: 0.006, 83: 0.016, 95: 0.011, 74: 0.013, 79: 0.013, 55: 0.013, 93: 0.011, 78: 0.007, 90: 0.01, 33: 0.01, 72: 0.014, 18: 0.005, 61: 0.013, 22: 0.01, 37: 0.012, 92: 0.017, 98: 0.012, 23: 0.006, 14: 0.009, 38: 0.008, 94: 0.005, 65: 0.01, 76: 0.019, 20: 0.007, 80: 0.014, 68: 0.009, 46: 0.009, 26: 0.007, 15: 0.015, 28: 0.009, 3: 0.009, 85: 0.011, 82: 0.018, 57: 0.012, 45: 0.008, 48: 0.015, 60: 0.012, 43: 0.011, 77: 0.007, 47: 0.013, 10: 0.01, 34: 0.009, 42: 0.008, 17: 0.012, 13: 0.011, 39: 0.007, 31: 0.01, 5: 0.009, 41: 0.011, 9: 0.01, 81: 0.011, 29: 0.007, 36: 0.011, 1: 0.013, 30: 0.016, 50: 0.011, 62: 0.01, 87: 0.011, 63: 0.013, 67: 0.008, 19: 0.006, 11: 0.009, 44: 0.008, 64: 0.007, 96: 0.013, 97: 0.012, 69: 0.005, 6: 0.012, 52: 0.005, 16: 0.008, 73: 0.009, 75: 0.01, 35: 0.005, 84: 0.008, 21: 0.012, 24: 0.008, 49: 0.01, 86: 0.008, 99: 0.01, 40: 0.013, 27: 0.002, 56: 0.012, 32: 0.008, 4: 0.005, 54: 0.007, 88: 0.013, 7: 0.011, 66: 0.011, 91: 0.005, 25: 0.003}
from collections import Counter
import numpy as np

x = np.random.randint(low=1, high=100, size=1000)
counts = Counter(x)
total = sum(counts.values())
probability_mass = {k:v/total for k,v in counts.items()}
# Print each probability
print(str(probability_mass))

You don't need to calculate the probability mass by yourself if you're using matplotlib as the hist function already does that for you:

from collections import Counter
import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(low=1, high=100, size=1000)
# create a list of elements repeating as many times as its count
counts = list(Counter(x).elements())
plt.hist(counts, density=True, edgecolor='black')

归一化

If you don't want the density (normalized version), don't pass the density flag:

plt.hist(counts, edgecolor='black')

未标准化

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM