Search code examples
pythonbokeh

How to change Bokeh figure segment line width dynamically


I have a figure containing a segment :

p = figure(height=500, tools="pan,wheel_zoom,box_zoom,reset", x_axis_type='datetime', y_axis_location="right")

high_low_segment = p.segment(x0='time', y0='low', x1='time', y1='high', line_width=1, color='black', source=source)

(this is from the OHLC example)

I'm wondering how to change the line_width of the segment dynamically - namely when the x-scale changes (catching RangesUpdate event) - to scale accordingly to the zoom level.

I'm new to bokeh so I'm not sure if it's possible directly in Python, if I have to wire some JS or if it's just impossible this way. Maybe I should completely rewrite my figure or remove the segment and add a new one ?

Environment:

  • bokeh 2.4.0
  • python 3.9.7

Solution

  • In fact, segment is the wrong choice to achieve scalable size rectangles. Using Rect glyph with data height and width units works out of the box.

    p.rect(x='time',
           y='high_low_y',
           fill_color='black',
           height='high_low_height',
           width=20000,
           line_width=0,
           angle=0,
           height_units='data',
           width_units='data',
           source=source
           )
    p.rect(x='time',
           y='open_close_y',
           fill_color='open_close_color',
           height='open_close_height',
           width=60000,
           line_width=0,
           line_color='black',
           angle=0,
           height_units='data',
           width_units='data',
           source=source
           )