Install the Pygal plug-in using the PIP install Pygal in the console. Reference URL: http://www.pygal.org/en/stable/installing.html
2. In the die.py document,
From random Import Randint class Die (): "" "" "is the
class of a dice" ""
def __init__ (self, num_sides=6): "" "
dice defaults to 6 sides" " "
self.num_sides = num_sides
def roll (self):" ""
returns a random value between 1 and the number of die Faces "" "Return
randint (1, self.num_ Sides
3. In the die_visual.py file
Import Pygal from
die import die
# Create a D6
die = Die ()
# Roll several dice and store the results in a list
results = [] for
Roll_ Num in range (1000):
outcome = Die.roll ()
results.append (Result)
# parse Results
frequencies = [] for
value In range (1, die.num_sides+1):
frequency = Results.count (value)
frequencies.append (frequency)
print ( frequencies)
# Visualize the result
hist = Pygal. Bar ()
hist.title = "Results of rolling one D6 1000 times."
Hist.x_labels = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ']
hist.x_title = "Result"
Hist.y_title = "Frequency of result"
hist.add (' D6 ', frequencies)
#将结果导出到一个svg文件中, in the browser can preview the view
hist.render_to_file (' die_visual.svg ')
4. The code run produces die_visual.svg files, which are opened in the browser as follows.