【问题标题】:fitting a cumulative line to histogram with matplotlib使用 matplotlib 将累积线拟合到直方图
【发布时间】:2013-10-14 08:38:04
【问题描述】:

我创建了下面的直方图:

并且想知道是否可以仅绘制顶部边缘(黑色)而不是绘制整个图形(蓝色)?

或者只是拟合线以匹配分布的顶部?

我的代码是:

plt.hist(histogramData, bins=200, normed=True, cumulative=True, edgecolor='b', facecolor='None')

我尝试删除 'edgecolor' 和 'facecolor' 但似乎不起作用...

感谢您的帮助!

【问题讨论】:

  • 或许能够同时绘制两者也会很有趣。喜欢this in d3.js

标签: python matplotlib histogram


【解决方案1】:

我认为 pylabs 直方图代码使用 numpys np.histogram() 函数,产生箱和计数;因此,如果您将它与标准的plot() 命令一起使用,您就完成了(只需记住还要在np.histogram() 的计数上执行np.cumsum() 以获得累积外观)。

编辑: 关于评论,我引用numpy.histogram()documentation

返回

hist : 数组

直方图的值。有关可能语义的描述,请参见 normed 和 weights。

bin 边:dtype 浮点数组

返回 bin 边缘 (length(hist)+1)。

因此,以所需的方式绘制数据:

hist, bins = np.histogram(data, bins=200)
plt.plot( bins[:-1], np.cumsum(hist) )

或者如果您想要更精确,您甚至可以将数据值放在 bin 中心:

offset = bins[1:]-bins[:-1]
plt.plot( bins[:-1]+offset, np.cumsum(hist) )

【讨论】:

  • 谢谢。我试过了,看起来箱数和计数不同(200 和 201),我无法绘制数据...
  • @kate88 实际上,bin 是 bin 边缘的值,而不是中心值(请参阅np.hist 文档)。正如你所看到的,我因此没有获取所有的 bin,而是计算 bin 的中心位置以绘制点图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-01
  • 2012-12-07
相关资源
最近更新 更多