使用散景的加权数据的直方图

时间:2016-12-25 15:55:11

标签: python histogram bokeh

如何使用散景生成加权数据的直方图?

在matplotlib中,可以执行以下操作:

from pylab import hist
x = randn(100); w = rand(100)
hist(x, weights=w)

如何在散景中做同样的事情?

1 个答案:

答案 0 :(得分:0)

基本上,你将权重传递给numpy的直方图函数。

import numpy as np

from bokeh.layouts import gridplot
from bokeh.plotting import figure, show, output_file

p1 = figure(title="Normal Distribution (μ=50, σ=20) and normal weights",tools="save",
        background_fill_color="#E8DDCB")

measured = np.random.normal(50, 20, 1000)

myweights = np.random.normal(0, 1, 1000)
hist, edges = np.histogram(measured, density=True, bins=10,weights=myweights)

x = np.linspace(-2, 2, 1000)
p1.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:],
    fill_color="#036564", line_color="#033649")
p1.legend.location = "top_left"
p1.xaxis.axis_label = 'x'
p1.yaxis.axis_label = 'Pr(x)'

show(p1)