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

histogram for weighted data using bokeh

如何使用bokeh为加权数据生成直方图?

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

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

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

基本上,您将权重传递给 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)