setting properties of a rect in a figure

Hi,
I am trying to work out how individual rects can be managed in the code below, I have some rects in a figure, I would like to change the fill_color for one of them, for example the second one. What is the best way to do this?

code for figure

from bokeh.plotting import figure

from bokeh.io import push_notebook, output_notebook, show

from math import *

output_notebook()

p1 =figure()

p1.rect([1,2,3,4,5], [1,2,3,4,5], width=.1, height=.1, color=“blue”)

h3 = show(p1, notebook_handle=True)

I tried adding the rects one by one in a loop and storing the references.

Then using these references to adjust properties but it is very slow.

Thank you.

Hey,

If you know the colors on forehand, the most straightforward way is to specify them as a collection, just as you do with the x, and y values. For example:

xs = [1,2,3,4,5]
ys = [1,2,3,4,5]

colors = [‘blue’] * len(xs)
colors[1] = ‘red’

p1 =figure(height=300, width=300)

r = p1.rect(xs, ys, color=colors, width=.25, height=.25)

``

Note how i captured the result of p1.rect, this gives you access to the underlying ColumnDataSource attached to those rects. So if you want to change the colors after initial plotting, do something like:

r.data_source.data[‘fill_color’][2] = ‘green’ # set the third rect to be green

``

Here is a notebook which shows the entire example:

Regards,
Rutger

···

On Wednesday, November 23, 2016 at 9:46:46 PM UTC+1, [email protected] wrote:

Hi,
I am trying to work out how individual rects can be managed in the code below, I have some rects in a figure, I would like to change the fill_color for one of them, for example the second one. What is the best way to do this?

code for figure

from bokeh.plotting import figure

from bokeh.io import push_notebook, output_notebook, show

from math import *

output_notebook()

p1 =figure()

p1.rect([1,2,3,4,5], [1,2,3,4,5], width=.1, height=.1, color=“blue”)

h3 = show(p1, notebook_handle=True)

I tried adding the rects one by one in a loop and storing the references.

Then using these references to adjust properties but it is very slow.

Thank you.

Thank you! That it exactly what I was looking to do. Thank you so much.

···

On Thursday, November 24, 2016 at 2:55:46 AM UTC-5, Rutger Kassies wrote:

Hey,

If you know the colors on forehand, the most straightforward way is to specify them as a collection, just as you do with the x, and y values. For example:

xs = [1,2,3,4,5]
ys = [1,2,3,4,5]

colors = [‘blue’] * len(xs)
colors[1] = ‘red’

p1 =figure(height=300, width=300)

r = p1.rect(xs, ys, color=colors, width=.25, height=.25)

``

Note how i captured the result of p1.rect, this gives you access to the underlying ColumnDataSource attached to those rects. So if you want to change the colors after initial plotting, do something like:

r.data_source.data[‘fill_color’][2] = ‘green’ # set the third rect to be green

``

Here is a notebook which shows the entire example:
http://nbviewer.jupyter.org/gist/RutgerK/da6044708e57d0d80e956b3e90fc308c

Regards,
Rutger

On Wednesday, November 23, 2016 at 9:46:46 PM UTC+1, [email protected] wrote:

Hi,
I am trying to work out how individual rects can be managed in the code below, I have some rects in a figure, I would like to change the fill_color for one of them, for example the second one. What is the best way to do this?

code for figure

from bokeh.plotting import figure

from bokeh.io import push_notebook, output_notebook, show

from math import *

output_notebook()

p1 =figure()

p1.rect([1,2,3,4,5], [1,2,3,4,5], width=.1, height=.1, color=“blue”)

h3 = show(p1, notebook_handle=True)

I tried adding the rects one by one in a loop and storing the references.

Then using these references to adjust properties but it is very slow.

Thank you.