1

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
Pierre Mardon
  • 727
  • 8
  • 25

2 Answers2

0

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
       )

Pierre Mardon
  • 727
  • 8
  • 25
0

with some widget via js_on_change or on_change. for example : Slider

slider = Slider(start=0, end=10, value=1, step=1, title="line width")
spinner.js_link('value', high_low_segment.glyph, 'line_width')

or with on_change

def functionname(attr, old, new):
    high_low_segment.glyph.line_width = slider.value

slider = Slider(start=0, end=10, value=1, step=1, title="line width")
spinner.on_change('value', functionname)
  • This is useful but doesn't fully answer the question: could you adapt your examples to be triggered by a `RangesUpdate` event ? – Pierre Mardon Sep 23 '21 at 06:59