简体   繁体   中英

Python Matplotlib pyplot histogram

I am plotting a histogram of a fairly simple simulation. On the histogram the last two columns are merged and it looks odd. Please finde attached below the code and the plot result.

Thanks in advance!

   import numpy as np
   import random
   import matplotlib.pyplot as plt

   die = [1, 2, 3, 4, 5, 6]
   N = 100000
   results = []
   # first round
   for i in range(N):
       X1 = random.choice(die)
       if X1 > 4:
           results.append(X1)
       else:
           X2 = random.choice(die)
           if X2 > 3:
               results.append(X2)
           else:
               X3 = random.choice(die)
               results.append(X3)

   plt.hist(results)
   plt.ylabel('Count')
   plt.xlabel('Result');
   plt.title("Mean results: " + str(np.mean(results)))
   plt.show()

The output looks like this. I dont understand why the last two columns are stuck together.

直方图

Any help appreciated!

You need to tell matplotlib that you want the histogram to match the bins. Otherwise matplotlib choses the default value of 10 for you - in this case, that doesn't round well.

# ... your code ...

plt.hist(results, bins=die)  # or bins = 6
plt.ylabel('Count')
plt.xlabel('Result');
plt.title("Mean results: " + str(np.mean(results)))
plt.show()

Full documentation is here: https://matplotlib.org/3.2.2/api/_as_gen/matplotlib.pyplot.hist.html

By default, matplotlib divides the range of the input into 10 equally sized bins. All bins span a half-open interval [x1,x2) , but the rightmost bin includes the end of the range. Your range is [1,6] , so your bins are [1,1.5) , [1.5,2) , ..., [5.5,6] , so all the integers end up in the first, third, etc. odd-numbered bins, but the sixes end up in the tenth (even) bin.

To fix the layout, specify the bins:

# This will give you a slightly different layout with only 6 bars
plt.hist(results, bins=die + [7])
# This will simulate your original plot more closely, with empty bins in between
plt.hist(results, bins=np.arange(2, 14)/2)

The last bit generates the number sequence 2,3,...,13 and then divides each number by 2, which gives 1, 1.5, ..., 6.5 so that the last bin spans [6,6.5] .

it doesn't, but you can try it.

import seaborn as sns
sns.distplot(results , kde = False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM