繁体   English   中英

如何在 Python 中使用 Matplotlib 绘制直方图以获取概率?

[英]How to plot a histogram using Matplotlib in Python taking probability?

我使用 randint 函数计算概率。 但我在直方图中面临问题以显示我的概率。 还附加了我收到错误的 snap 我的概率输出是这样的。

{60: 0.013, 6: 0.016, 99: 0.01, 25: 0.006, 45: 0.017, 51: 0.009, 72: 0.011, 8: 0.015, 10: 0.015, 82: 0.011, 50: 0.014, 43: 0.012, 52: 0.011, 74: 0.015, 12: 0.015, 39: 0.01, 89: 0.014, 7: 0.009}

我的python代码。

from collections import Counter
import numpy as np
import matplotlib.pyplot as plt

hist = np.random.randint(low=1, high=100, size=1000)
counts = Counter(hist)
total = sum(counts.values())
hist = {k:v/total for k,v in counts.items()}


num_bins = 10
n, bins, patches = plt.hist(hist, num_bins, normed=1, facecolor='blue', alpha=0.5)
#plt.plot(bins, hist, 'r--')
plt.xlabel('Grades')
plt.ylabel('Probability')
plt.title('Histogram of Students Grade')
plt.subplots_adjust(left=0.15)
plt.show()

如果你只是注释掉plt.plot(bins, hist, 'r--')你会得到这个图:

在此处输入图片说明

这是你想要的?

如果你想使用nbinspatches你可以 参考Matplotlib 文档中的 这个例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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