matplotlib.pyplot plt.hist() 绘制概率图时,概率和不为1的问题(已解决)

数据是10,000个随机投掷的骰子

uni_data = np.random.randint(1, 7, 10000)

直方图
matplotlib.pyplot plt.hist() 绘制概率图时,概率和不为1的问题(已解决)
直接用density=1,y轴的概率值是不对的
matplotlib.pyplot plt.hist() 绘制概率图时,概率和不为1的问题(已解决)
方法一:用bins修正,同时修正了x刻度对不齐的问题

plt.hist(uni_data,bins=np.arange(0.5, 7.5), edgecolor='w',density=1) 

matplotlib.pyplot plt.hist() 绘制概率图时,概率和不为1的问题(已解决)

方法二:给bins增加权重(参考

weights = np.ones_like(uni_data )/float(len(uni_data ))
plt.hist(uni_data, weights=weights)

matplotlib.pyplot plt.hist() 绘制概率图时,概率和不为1的问题(已解决)

上一篇:matplotlib.pyplot的全函数解释 API


下一篇:【原创】PageAdminCMS 前台SQL注入漏洞(1)