how can i reference a specific rect in a Bokeh plot?

Question:

I’m working on a plot with ornamentations that draw rectangles over the plot to highlight some sections.

I’d like to find a reference for a rect that is plotted on top so that i can move it, unrelated to the data plotted. I cant seem to figure out how to do that. If there were multiple glyphs how would i be able to reference each one?

Lets say that I had a number of highlight rectangles overlaying my plot. and my plot was plotted in rectangles, how would i know to find one of the highlights?

x = [x*0.005 for x in range(0, 200)]
y = x      

source = ColumnDataSource(data=dict(x=x, y=y))

plot = figure(width=400, height=400, x_range=(0, 1), y_range=(0, 1))

plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

# the points to be plotted
x1 = .30
y1 = .30
w1 = .40
h1 = .20
 
# plotting the graph
plot.rect(x1, y1,w1,h1)

slider = Slider(start=0.1, end=4, value=1, step=.1, title="power")
slider.js_link('value', plot,'x1')
layout = column(slider, plot)

show(layout)

the js_link call doesn’t find the x1 in the Rect under the plot structure. plot.rect doesn’t work of course.

how can i reference the rect?

thanks!

Asked By: shigeta

||

Answers:

As bigreddot mentioned in the comment, you can address the rect in the list of the renderers of you plot object. This depends on the order you addad the renderers. To connect the x value of the rect to a slider value you can use plot.renderers[1].glyph.

The example below gives you the opportunity to move the rect by changing the center with to sliders.

from bokeh.plotting import show, figure, output_notebook
from bokeh.models import ColumnDataSource, Slider
from bokeh.layouts import column
output_notebook()

x = [0, 1]
y = [0, 1]      

source = ColumnDataSource(data=dict(x=x, y=y))

plot = figure(width=400, height=400, x_range=(0, 1), y_range=(0, 1))

plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)

# the points to be plotted
x1 = .30
y1 = .30
w1 = .40
h1 = .20
 
# plotting the graph
plot.rect(x1, y1,w1,h1)

slider_x = Slider(start=0.1, end=1, value=x1, step=.1, title="x")
slider_x.js_link('value', plot.renderers[1].glyph,'x')
slider_y = Slider(start=0.1, end=1, value=y1, step=.1, title="y")
slider_y.js_link('value', plot.renderers[1].glyph,'y')
layout = column(slider_x, slider_y, plot)

show(layout)
Answered By: mosc9575
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.