Search code examples
pythonpython-3.xpandasmatplotlibhistogram

bins option in plt.hist MatPlotlib


enter image description here

In the histogram above there are 5 bins despite specifying 6 in the 'bins=' option within matplotlib.pyplot.hist(). I thought the 'bins=' was supposed to force the program to plot as many number of bins as specified.

Will greatly appreciate if someone could clarify.

Here's the associated code:

import random
def simulate_diceRolls():
    count_dice_faces=[0, 0, 0, 0, 0, 0] 
    n = 1000 
    for i in range(n):
      result = random.randint(1, 6)
      count_dice_faces[result - 1] = count_dice_faces[result - 1] + 1

    simulation_dictionary = dict()
    for i in range(len(count_dice_faces)):
        simulation_dictionary[i+1] = count_dice_faces[i]

    plt.hist(count_dice_faces,bins=6)
    print(count_dice_faces)
    #the following code plots a nicer looking hisotgram but does not satisfy the using 'bin='
    #requirement in the question
    #plt.bar(simulation_dictionary.keys(), simulation_dictionary.values(), width=1,color='g',ec='black')
    plt.show()
simulate_diceRolls()

Solution

  • The bins parameter specifies the number of boxes into which your data will be split. You can specify it as an integer or as a list of the edges of the bin.

    For example, here we are requesting 20 bins:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.random.randn(1000)
    plt.hist(x, bins=20)
    

    enter image description here

    You have 6 bins on the given histogram. The thing is that 4 bins merged into one large one.

    enter image description here