Python:Matplotlib-概率质量函数作为直方图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python:Matplotlib-概率质量函数作为直方图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1391字,纯文字阅读大概需要2分钟。
内容图文
![Python:Matplotlib-概率质量函数作为直方图](/upload/InfoBanner/zyjiaocheng/693/a4b1216cfa5c4e8496a5dc4b383647f8.jpg)
我想在同一张图上绘制直方图和折线图.但是,要做到这一点,我需要将直方图作为概率质量函数,因此我想在y轴上具有概率值.但是,我不知道该怎么做,因为使用normed选项没有帮助.以下是我的源代码和使用数据的先睹为快.我将非常感谢所有建议.
data = [12565, 1342, 5913, 303, 3464, 4504, 5000, 840, 1247, 831, 2771, 4005, 1000, 1580, 7163, 866, 1732, 3361, 2599, 4006, 3583, 1222, 2676, 1401, 2598, 697, 4078, 5016, 1250, 7083, 3378, 600, 1221, 2511, 9244, 1732, 2295, 469, 4583, 1733, 1364, 2430, 540, 2599, 12254, 2500, 6056, 833, 1600, 5317, 8333, 2598, 950, 6086, 4000, 2840, 4851, 6150, 8917, 1108, 2234, 1383, 2174, 2376, 1729, 714, 3800, 1020, 3457, 1246, 7200, 4001, 1211, 1076, 1320, 2078, 4504, 600, 1905, 2765, 2635, 1426, 1430, 1387, 540, 800, 6500, 931, 3792, 2598, 5033, 1040, 1300, 1648, 2200, 2025, 2201, 2074, 8737, 324]
plt.style.use('ggplot')
plt.rc('xtick',labelsize=12)
plt.rc('ytick',labelsize=12)
plt.xlabel("Incomes")
plt.hist(data, bins=50, color="blue", alpha=0.5, normed=True)
plt.show()
解决方法:
据我所知,matplotlib没有内置此功能.但是,复制起来很容易
import numpy as np
heights,bins = np.histogram(data,bins=50)
heights = heights/sum(heights)
plt.bar(bins[:-1],heights,width=(max(bins) - min(bins))/len(bins), color="blue", alpha=0.5)
编辑:这是a similar question的另一种方法:
weights = np.ones_like(data)/len(data)
plt.hist(data, bins=50, weights=weights, color="blue", alpha=0.5, normed=False)
内容总结
以上是互联网集市为您收集整理的Python:Matplotlib-概率质量函数作为直方图全部内容,希望文章能够帮你解决Python:Matplotlib-概率质量函数作为直方图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。