Search code examples
pythonbokeh

bokeh hbar example with categorical values = white box


I've got code for a simple hbar plot that is stripped down to what I think should be, but shows up as a white box. (I can get the simple example of a line plot working so I know the headers are set up correctly.)

        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)

The error in the console is "[Bokeh] could not set initial ranges" If someone could point me to the documentation about anything needing to be added that would be helpful.


Solution

  • As you are working with categorical data, you need to assign a FactorRange for your y_range. This is done either by p.y_range=FactorRange(factors=fruits) or its shorthand version p.x_range=fruits.
    The following example shows the figure corectly:

    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