Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@AO8
Last active February 7, 2020 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AO8/0b02aa9cdab6b554e59cbd71551a4765 to your computer and use it in GitHub Desktop.
Save AO8/0b02aa9cdab6b554e59cbd71551a4765 to your computer and use it in GitHub Desktop.
Dynamic dice roll visualizer with Python 3 and Matplotlib.
import datetime
import random
import sys
from matplotlib import animation
import matplotlib.pyplot as plt
import seaborn as sns
def update(frame_number, rolls, faces, frequencies):
random.seed(datetime.datetime.now())
for i in range(rolls):
frequencies[random.randrange(1, 7) - 1] += 1
plt.cla()
axes = sns.barplot(faces, frequencies, palette="bright")
axes.set_title(f"Dice frequencies for {sum(frequencies):,} rolls")
axes.set(xlabel="Dice Value", ylabel="Frequency")
axes.set_ylim(top=max(frequencies) * 1.15)
for bar, frequency in zip(axes.patches, frequencies):
text_x = bar.get_x() + bar.get_width() / 2.0
text_y = bar.get_height()
text = f"{frequency:,}\n{frequency / sum(frequencies):.3%}"
axes.text(text_x, text_y, text, ha="center", va="bottom")
number_of_frames = 1000 # for command line args use int(sys.argv[1]) instead
rolls_per_frame = 1 # for command line args use int(sys.argv[2]) instead
sns.set_style("darkgrid")
figure = plt.figure("Rolling a 6-sided dice")
values = list(range(1, 7))
frequencies = [0] * 6
die_animation = animation.FuncAnimation(
figure, update, repeat=False, frames=number_of_frames,
interval=33, fargs=(rolls_per_frame, values, frequencies))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment