0

enter image description hereRepresenting an experiment with two dices using matplotlib - wrong representation I have two 6-sided dices; I would like to represent the results after 1000 experiments using a matplotlib histogram. I have two problems:

  1. I wish the x axis would show all possible results from 1 to 12 , one by one; but I have only the even number in the axis

  2. The histogram is empty toward the middle, where the result for 7 should be displayed.

    Coding:

    #Dices, from book Python Crash course 2nd ed.

    from random import randint

    class Dice(): """Represent a dice, which can be rolled."""

     def __init__(self, sides):
         """Initialize the die."""
         self.sides = sides
    
     def roll_die(self):
         """Return a number between 1 and the number of sides."""
         return randint(1, self.sides)
    

    #Make a 6-sided die, and show the results of 1000 rolls. #dice d6a

    d6a = Dice(6)

    resultd6a = []

    for roll_num in range(1000):

     result = d6a.roll_die()
    
     resultd6a.append(result)
    

    #dice d6b

    d6b = Dice(6)

    resultd6b = []

    for roll_num in range(1000):

     result = d6b.roll_die()
    
     resultd6b.append(result)
    

    #add dices

    sum_list = [a + b for a, b in zip(resultd6a, resultd6b)]

    print("\n10000 rolls of 2 6-sided dies:")

    print(sum_list)

    #Matplotlib display

    %pylab inline

    import matplotlib.pyplot as plt

    plt.hist(sum_list, alpha = 1, color = 'blue', label = '6 sided dice', bins = 12)

    plt.axis([0, 12, 0, 200])

    plt.xlabel("dice number")

    plt.ylabel("repetitions")

    plt.legend(loc = 'best')

    plt.title("frequency of different dices")

    plt.show()

Ernestico
  • 1
  • 1

0 Answers0