0

I want to display a scattering plot using holoviews and update the plot every 10 seconds. Therefore I wrote a function "BokehDisplay" to plot the scattering plot without updating, then I added another function "DynamicDisplay" to update the scattering plot every 10 seconds. However, the scattering plot only showed up when I ran the first function, but not when I ran the second function. Any hints would be highly appreciated! Thanks!

from datetime import datetime
import holoviews as hv
import numpy as np
import time
hv.extension('bokeh') 

def BokehDisplay():
    x, y = [], []
    with open("evening_commute_time.txt", "r") as f:
        for line in f:
            x.append(line[:19])
            y.append(int(line[27:29]))
    x_time = np.array(x, dtype = np.datetime64)
    scatter =  hv.Scatter((x_time,y), kdims = ['starting time'], vdims = ['Commute Time (min)'] )
    return scatter

def DynamicDisplay(flag):
    while flag > 0:
        BokehDisplay()
        time.sleep(10)
        flag -= 1
CathyQian
  • 1,081
  • 15
  • 30
  • jlstevens answered your question below, but for future reference, the reason BokehDisplay() works if used by itself in a Jupyter Notebook cell is that it returns a HoloViews object, and if something is returned in a notebook cell Jupyter will display it if it can. But there's no return value to DynamicDisplay(), and thus nothing is ever displayed in that case. – James A. Bednar Sep 21 '17 at 13:12

1 Answers1

2

You need to wrap your callable returning the Scatter in a DynamicMap so you can update it periodically with the event method:

import time
from holoviews.streams import Stream

dmap = hv.DynamicMap(BokehDisplay, streams=[Stream.define('next')()])
dmap  # Display the DynamicMap here

while True:  # In a new notebook cell
   time.sleep(10)
   dmap.event()

Hope that helps.

jlstevens
  • 201
  • 1
  • 2
  • Here if a cell ends in "dmap", the plot will get displayed, and the dynamic updating is then handled by a *separate* cell that does *not* return a value... – James A. Bednar Sep 21 '17 at 13:14