Using a slider to update index into array when plotting data?

Hello! I’m having quite a bit of trouble doing something (that should be) very simple in Bokeh. My goal is to animate the movement of a circle on a set of axes. The circles X / Y data is precalculated and stored in a dataframe. My approach, pasted below, is to use an ipython widget. Unfortunately, the code doesn’t update!

I’ve pasted an example of what I’m trying to do below. Note that I’m using a parametric equation below, but only for convenience. In my final code, the data must be precalculated, and indexed into within update().

Thanks!

  • gD

import numpy as np

from bokeh.io import push_notebook

import bokeh.plotting as bkP

import bokeh.models as bkM

from ipywidgets import interact

import numpy as np

bkP.output_notebook()

xs = np.linspace(0, 2*np.pi, 2000)

ys = np.sin(xs)

frame = 0

x = xs[frame]

y = np.sin(x)

p = bkP.figure(plot_width=300,plot_height=300)

frame = 0

theCircle = p.circle(,

[y],

name = ‘circle’,size=10)

bkP.show§

def update(frame=0):

theCircle.data_source.data[‘x’] = [xs[frame]]

theCircle.data_source.data[‘y’] = [ys[frame]]

push_notebook()

bkP.show§ # this seems suspect

interact(update, frame=(0,len(xs),1))

``

Hi Gabriel,

The first note is that you don't need (and should not have) the "show" call in the "update function". In general it defeats the purpose of the efficient notebook comms, but there is also a current bug (to be fixed in 0.11.1) where only the last shown plot gets updated (so showing a second time will bump into that issue).

But also, Bokeh's "show" and Jupyter's "interact" both use the notebook cell publishing machinery to display themselves. Putting both in one cell means that one's output will "overwrite" the other's. So, they need to go in different cells. I am not aware of any way around this, unfortunately. When I split up your example in to a few cells, and delete the extra "show" in the callback, then it works great.

Thanks,

Bryan

···

On Jan 15, 2016, at 4:49 PM, Gabriel Diaz <[email protected]> wrote:

Hello! I'm having quite a bit of trouble doing something (that should be) very simple in Bokeh. My goal is to animate the movement of a circle on a set of axes. The circles X / Y data is precalculated and stored in a dataframe. My approach, pasted below, is to use an ipython widget. Unfortunately, the code doesn't update!

I've pasted an example of what I'm trying to do below. Note that I'm using a parametric equation below, but only for convenience. In my final code, the data must be precalculated, and indexed into within update().

Thanks!
- gD

import numpy as np
from bokeh.io import push_notebook
import bokeh.plotting as bkP
import bokeh.models as bkM

from ipywidgets import interact
import numpy as np

bkP.output_notebook()

xs = np.linspace(0, 2*np.pi, 2000)
ys = np.sin(xs)

frame = 0

x = xs[frame]
y = np.sin(x)

p = bkP.figure(plot_width=300,plot_height=300)

frame = 0
theCircle = p.circle(,
                     [y],
                     name = 'circle',size=10)
bkP.show(p)

def update(frame=0):

    theCircle.data_source.data['x'] = [xs[frame]]
    theCircle.data_source.data['y'] = [ys[frame]]
    
    push_notebook()
    bkP.show(p) # this seems suspect
    
interact(update, frame=(0,len(xs),1))

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/82c02390-fcd9-4e09-b337-167fc9c3e3e9%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Indeed, it works like a charm, now. Thanks, Bryan!

···

On Friday, January 15, 2016 at 5:49:53 PM UTC-5, Gabriel Diaz wrote:

Hello! I’m having quite a bit of trouble doing something (that should be) very simple in Bokeh. My goal is to animate the movement of a circle on a set of axes. The circles X / Y data is precalculated and stored in a dataframe. My approach, pasted below, is to use an ipython widget. Unfortunately, the code doesn’t update!

I’ve pasted an example of what I’m trying to do below. Note that I’m using a parametric equation below, but only for convenience. In my final code, the data must be precalculated, and indexed into within update().

Thanks!

  • gD

import numpy as np

from bokeh.io import push_notebook

import bokeh.plotting as bkP

import bokeh.models as bkM

from ipywidgets import interact

import numpy as np

bkP.output_notebook()

xs = np.linspace(0, 2*np.pi, 2000)

ys = np.sin(xs)

frame = 0

x = xs[frame]

y = np.sin(x)

p = bkP.figure(plot_width=300,plot_height=300)

frame = 0

theCircle = p.circle(,

[y],

name = ‘circle’,size=10)

bkP.show§

def update(frame=0):

theCircle.data_source.data[‘x’] = [xs[frame]]

theCircle.data_source.data[‘y’] = [ys[frame]]

push_notebook()

bkP.show§ # this seems suspect

interact(update, frame=(0,len(xs),1))

``