Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pzelasko/d73077db3e0763efb4568de6a70504eb to your computer and use it in GitHub Desktop.
Save pzelasko/d73077db3e0763efb4568de6a70504eb to your computer and use it in GitHub Desktop.
Interactive and clickable plots using Panel, Holoviz and Bokeh in Jupyter Notebooks
import numpy as np
import panel as pn
pn.extension()
import holoviews as hv
hv.notebook_extension("bokeh")
# hv.extension('matplotlib')
from holoviews.streams import Stream, Params
from scipy.io import wavfile
from scipy.signal import spectrogram
rate, data = wavfile.read('/filepath.wav')
width = 1000
audio = pn.pane.Audio(data, sample_rate=rate, name='Audio', throttle=500)
time = np.linspace(0, len(data) / rate, num=len(data))
line_plot = hv.Curve((time, data), ["time (s)","amplitude"]).opts(width=width)
f, t, sxx = spectrogram(data, rate)
spec_gram = hv.Image((t, f, np.log10(sxx)), ["time (s)","frequency (hz)"]).opts(width=width)
def interactive_play(x,y,t):
if x is None:
return hv.VLine(t).opts(color='green')
else:
audio.time = x
return hv.VLine(x).opts(color='green')
stream = Params(parameters=[audio.param.time], rename={'time': 't'})
tap = hv.streams.SingleTap(transient=True)
dmap_time = hv.DynamicMap(interactive_play, streams=[stream, tap])
pn.Column( audio, (spec_gram * dmap_time), (line_plot * dmap_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment