Updating multiple patches

I’m using a ColumnDataSource to update multiple patches with bokeh server. By multiple patches, I’m referring specifically to the ones using ‘nan’ to plot multiple patches, which are shown in the documentation like this:

p.patch([1, 2, 3, nan, 4, 5, 6], [6, 7, 5, nan, 7, 3, 6], alpha=0.5, line_width=2)

I was trying to update my values using a slider, and had this function:

def slider_update(attrname, old, new):

val = slider.value

x = sources[val].data

y = patches.data_source.data

y['a'] = x['a']

y['b'] = x['b']

However, I read one of the other posts that mentioned that it was better to change the whole data source at the same time (patches.data_source.data = sources[val].data).

When I tried to update the whole source instead of specific columns, I ran into this error:

Error thrown from periodic callback: ValueError(‘Out of range float values are not JSON compliant: nan’,)

I’m aware of the open issue #4472, but I was wondering if there was any way to work around this error as well, since the ‘nan’ values aren’t in my data. The ‘nan’ values from this error are the ones used to draw the separate multiple patches. It would be great to be able to update the whole data source at the same time, since I did notice it had better performance than updating separate columns.

I have a small example that produces this error as well:

import pandas as pd

from bokeh.plotting import figure, output_server

from bokeh.models import ColumnDataSource, Slider

from bokeh.io import curdoc

from bokeh.layouts import layout

p = figure(plot_width=400, plot_height=400)

nan = float(‘nan’)

x = [[1, 2, 3, nan, 4, 5, 6]]

y = [[6, 7, 5, nan, 7, 3, 6]]

color = [‘red’]

color2 = [‘blue’]

df1 = pd.DataFrame({‘x’:x, ‘y’:y, ‘color’:color})

df2 = pd.DataFrame({‘x’:x, ‘y’:y, ‘color’:color2})

sources = {}

sources[0] = ColumnDataSource(df1)

sources[1] = ColumnDataSource(df2)

patches = p.patches(‘x’, ‘y’, source=sources[0], color=‘color’)

def slider_update(attrname, old, new):

val = slider.value

patches.data_source.data = sources[val].data

slider = Slider(start=0, end=1, value=0, step=1, title=“value”)

slider.on_change(‘value’, slider_update)

layout = layout([p],[slider])


Any help would be appreciated. Thanks!