Plotly
installation
$ python3 -m pip install --user plotly
utility
die.py
from random import randint
class Die:
'''a model of a die'''
def __init__(self, sides=6):
# die sides can be customised
self.sides = sides
def roll(self):
# generate a random number between 1 and die sides
return randint(1, self.sides)
die_visual.py
from plotly.graph_objs import Bar, Layout
from plotly import offline
from die import Die
# create 2 dice
die_1 = Die()
die_2 = Die()
# roll dice for some times and store the sum in a list
results = []
for i in range(1000):
sum = die_1.roll() + die_2.roll()
results.append(sum)
# analyse the results
frequencies = []
max_num = die_1.sides+die_2.sides
for i in range(2, max_num+1):
frequency = results.count(i)
frequencies.append(frequency)
print(frequencies)
# visualise the results
x_values = list(range(2, max_num+1))
# create a list of bars, stored in data
data = [Bar(x=x_values, y=frequencies)]
# config x and y axis
x_axis_config = {'title': 'Result', 'dtick': 1}
y_axis_config = {'title': 'Frequency of Result'}
# create a Layout object, setting the title and utilising the configs above
my_layout = Layout(title='Results of Rolling Two D6 Dice 1000 Times', xaxis=x_axis_config, yaxis=y_axis_config)
# render the {data and layout}, storing the interactive figure as an html file
offline.plot({'data': data, 'layout': my_layout}, filename='d6_dice.html')