Bainly
Bainly

Reputation: 87

How do the x and y parameters in the Label object work for bokeh?

I've read the documentation for the Label class in Bokeh but the x and y parameters are quite confusing. Their behavior seems to change if you pass something to the x_units and y_units parameters but I don't understand what the units are supposed to be by default.

More specifically, I have a list of strings that I'm using for my x-axis:

    xlab = [
            'COREPCE2', 
            'COREPCE3', 
            'COREPCE4', 
            'COREPCE5', 
            'COREPCE6', 
            '',
            'T5YIE'
    ]
    p = figure(..., y_range = (0,.04), x_range = xlab)

If I wanted to draw basically anything else on the plot, I could just use those strings. For example I drew some lines like this:

    p.line(['COREPCE2', 'T5YIE'], [.02,.02], color = 'black', line_dash = 'dashed')
    p.line(['', ''], [0,.04], color = 'black')

And that works fine, this is the full chart.

Here's the issue though. I want to put a text label on the "COREPCE4" location of the x axis. If I try just passing the string for the x parameter in the Label class it just doesn't work:

    section = Label(x = 'COREPCE4', y = .03, text = 'Survey of Professional Forecasters: August 9, 2019')
    p.add_layout(section) 

It throws an error: ValueError: expected a value of type Real, got COREPCE4 of type str. I don't really know what units its expecting. Is there a way to make Bokeh recognize that I want to use the x-axis label as my x parameter in the same way I've done with the other glyphs?

Upvotes: 0

Views: 200

Answers (1)

bigreddot
bigreddot

Reputation: 34568

The propertied x_units, y_units, refer to screen (pixel) vs data-space (axis) units. As of Bokeh 1.3.4 the x and y properties of Label can only be set from floating point numbers, so they cannot be used directly with categorical coordinates. For now you should use LabelSet, even if you are only showing a single label, since it can work with categorical coordinates.

Upvotes: 1

Related Questions