如何用散景显示对数刻度的直方图?

How to show a histogram in log scale with bokeh?

问题都在标题里。我正在尝试使用从 bokeh.charts 导入的直方图 object,但不知道如何以对数比例显示它。在我的特殊情况下,我需要 x 轴和 y 轴都以对数刻度显示。

好的,似乎可以有对数刻度。但是,与其使用图表 API,我应该使用绘图 API:

import numpy as np
from bokeh.plotting import figure, show, output_notebook

output_notebook()

# generate random data from a powerlaw
measured = 1/np.random.power(2.5, 100000)

hist, edges = np.histogram(measured, bins=50)

p = figure(title="Power law (a=2.5)", x_axis_type="log", y_axis_type='log')
p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], line_color=None)

show(p)

感谢 bigreddot 的另一个问题的帮助!

请注意,@famagar 的上述解决方案不适用于最新版本的 bokeh(刚刚使用 0.12.14 尝试过——参见 this issue)。

问题是对数刻度cannot properly handle zeros

要解决这个问题,必须将 bottom 参数设置为某个 non-zero 值。 比如要得到和上图一样的结果 bottom=1:

p.quad(top=hist, bottom=1, left=edges[:-1], right=edges[1:], line_color=None)