gpt4 book ai didi

python - 使用 Bokeh 的加权数据直方图

转载 作者:行者123 更新时间:2023-12-05 07:45:48 25 4
gpt4 key购买 nike

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

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

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

如何在 Bokeh 中做同样的事情?

最佳答案

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

关于python - 使用 Bokeh 的加权数据直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41322548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com