具有分类值的散景 hbar 示例 = 白框

bokeh hbar example with categorical values = white box

我有一个简单的 hbar 图的代码,它被简化为我认为应该的样子,但显示为一个白框。 (我可以得到一个简单的线图示例,所以我知道 headers 设置正确。)

        from bokeh.embed import components
        from bokeh.plotting import figure

        fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
        counts = [5, 3, 4, 2, 4, 6]

        p = figure(plot_height=250, title="Fruit counts",
                   toolbar_location=None, tools="")

        p.hbar(y=fruits, right=counts)

        data, div = components(p)

控制台中的错误是“[Bokeh] 无法设置初始范围” 如果有人可以向我指出有关需要添加的任何内容的文档,那将会很有帮助。

因为你是 working with categorical data,你需要为你的 y_range 分配一个 FactorRange。这是由 p.y_range=FactorRange(factors=fruits) 或其 shorthand 版本 p.x_range=fruits.
完成的 下例正确显示该图:

from bokeh.embed import components
from bokeh.plotting import figure, show
from bokeh.models import FactorRange

fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
counts = [5, 3, 4, 2, 4, 6]

p = figure(y_range=FactorRange(factors=fruits), plot_height=250, title="Fruit counts",
            toolbar_location=None, tools="")

p.hbar(y=fruits, right=counts)

show(p)