When tossing a dice many many time all numbers tend to appear the same number of times, but if the number of throws is small then some numbers may appear more or less often than others, even considerably if the sample size is small enough.
This program shows a updating in real time graph of
SIDES bars (6 in default), each presenting how many times a number has appeared so far.
Please ignore that the numbers on the X axis are wrong, that is a minor bug-fix for later, all of the visualization is there.
My code feels a bit messy, please help in organization and simplification:
import random import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation SIDES = 6 HEIGHT = 100 def update_results(results): die_toss = random.randint(0, SIDES - 1) return [x + (1 if i == die_toss else 0) for i, x in enumerate(results)] def plot_points(ps): plt.scatter(*zip(*ps)) def animate(frameno): global x x = update_results(x) n, _ = np.histogram(x, SIDES) n = x for rect, h in zip(patches, n): rect.set_height(h) print(x, max(x) / float(min(x))) return patches if __name__ == "__main__": x = [ 1 for _ in range(SIDES) ] fig, ax = plt.subplots() n, bins, patches = plt.hist(x, SIDES, normed=1, facecolor='green', alpha=0.75) frames = 500 ani = animation.FuncAnimation(fig, animate) axes = plt.gca() axes.set_ylim([0, HEIGHT]) plt.show() plt.show() for i in range(100): plt.bar(range(SIDES), x) x =update_results(x) print(x) plt.show() plt.clf()