分类值为白色框的bokeh hbar示例

2024-05-18 12:34:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的hbar图的代码,它被精简为我认为应该是的,但显示为一个白框。(我可以得到一个简单的线图示例,这样我就知道标题设置正确了。)

        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]无法设置初始范围” 如果有人能给我指一下需要添加的文档,那会很有帮助


Tags: 代码fromimport标题示例bokehcomponentsembed
1条回答
网友
1楼 · 发布于 2024-05-18 12:34:34

由于您是working with categorical data,因此需要为y_range分配一个FactorRange。这可以通过p.y_range=FactorRange(factors=fruits)或其速记版本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)

enter image description here

相关问题 更多 >

    热门问题