Python:matplotlib - 概率质量函数作为直方图

标签 python python-2.7 matplotlib plot histogram

我想在同一张图上绘制直方图和线图。但是,为此我需要将直方图作为概率质量函数,因此我希望在 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 - 概率质量函数作为直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30889444/

相关文章:

python - 添加包含每个用户评分数的列,pandas

python - 任何只包含字母表、数字但不包含 Q、I、O 且没有其他字符的单词

python-2.7 - Tesseract 的路径错误

python - 访问 UnivariateSpline 曲线上的值

python - 如何显示多行而不是一行?

python - python中node.js中的req.url相当于什么

python - 在Python中创建多个记录器到同一模块中的不同输出

python - 字符串格式问题(括号或下划线)

python - 使用 pyscreenshot 截图

python:在顶部绘制带有函数线的直方图