import numpy as np import holoviews as hv hv.extension('bokeh') # Styles and plot options used in this user guide %opts Ellipse [bgcolor='white'] (color='black') %opts Image (cmap='viridis') %opts VLine HLine (color='red' line_width=2) %opts Path [show_grid=False bgcolor='white'] (color='black' line_dash='dashdot') %opts Area (fill_color='cornsilk' line_width=2 line_color='black') lin = np.linspace(-np.pi,np.pi,300) def lissajous(t, a,b, delta): return (np.sin(a * t + delta), np.sin(b * t)) def lissajous_curve(t, a=3,b=5, delta=np.pi/2): (x,y) = lissajous(t,a,b,delta) return hv.Path(lissajous(lin,a,b,delta)) * hv.VLine(x) * hv.HLine(y) hv.DynamicMap(lissajous_curve, kdims='t').redim.range(t=(-3.,3.)) from holoviews.streams import Stream, param Time = Stream.define('Time', t=0.0) hv.help(Time) Time = Stream.define('Time', t=param.Number(default=0.0, doc='A time parameter')) hv.help(Time) time_dflt = Time() print('This Time instance has parameter t={t}'.format(t=time_dflt.t)) time = Time(t=np.pi/4) print('This Time instance has parameter t={t}'.format(t=time.t)) dmap = hv.DynamicMap(lissajous_curve, streams=[time]) dmap + lissajous_curve(t=np.pi/4) dmap.event( t=0.2) time.event(t=-0.2) ls = np.linspace(0, 10, 200) xx, yy = np.meshgrid(ls, ls) XY = Stream.define('XY',x=0.0,y=0.0) def marker(x,y): return hv.Image(np.sin(xx)*np.cos(yy)) * hv.VLine(x) * hv.HLine(y) dmap = hv.DynamicMap(marker, streams=[XY()]) dmap dmap.event(x=-0.2, y=0.1) xs = np.linspace(-3, 3, 400) def function(xs, time): "Some time varying function" return np.exp(np.sin(xs+np.pi/time)) def integral(limit, time): curve = hv.Curve((xs, function(xs, time)))[limit:] area = hv.Area ((xs, function(xs, time)))[:limit] summed = area.dimension_values('y').sum() * 0.015 # Numeric approximation return (area * curve * hv.VLine(limit) * hv.Text(limit + 0.5, 2.0, '%.2f' % summed)) Time = Stream.define('Time', time=1.0) dmap=hv.DynamicMap(integral, kdims='limit', streams=[Time()]).redim.range(limit=(-3,2)) dmap dmap.event(time=8) def integral2(lim, t): 'Same as integral with different argument names' return integral(lim, t) dmap = hv.DynamicMap(integral2, kdims='limit', streams=[Time().rename(time='t')]).redim.range(limit=(-3.,3.)) dmap dmap=hv.DynamicMap(integral, kdims=['time','limit'], streams=[Time()]).redim.range(limit=(-3.,3.)) dmap dmap[1,0] + dmap.select(time=3,limit=1.5) + dmap[None,1.5] dmap.reset() # Reset the cache, we don't want the values from the cell above # TODO: redim the limit dimension to a default of 0 dmap.event(time=1) dmap.event(time=1.5) dmap.event(time=2) hv.HoloMap(dmap) def sample_distributions(samples=10, tol=0.04): np.random.seed(42) while True: gauss1 = np.random.normal(size=samples) gauss2 = np.random.normal(size=samples) data = (['A']*samples + ['B']*samples, np.hstack([gauss1, gauss2])) yield hv.BoxWhisker(data, 'Group', 'Value') samples+=1 sample_generator = sample_distributions() hv.DynamicMap(sample_generator) dmap = hv.DynamicMap(sample_generator, streams=[Stream.define('Next')()]) dmap for i in range(40): dmap.event() dmap.periodic(0.1, 1000, timeout=3) hv.HoloMap({i:next(dmap) for i in range(10)}, kdims='Iteration') defineXY = Stream.define('defineXY', x=0.0, y=0.0) class XY(Stream): x = param.Number(default=0.0, constant=True, doc='An X position.') y = param.Number(default=0.0, constant=True, doc='A Y position.') xy = XY(x=2,y=3) xy.update(x=4,y=50) xy.rename(x='xpos', y='ypos').contents xy = XY() xy.event(x=4,y=50) xy.contents def subscriber(xpos,ypos): print('The subscriber received xpos={xpos} and ypos={ypos}'.format(xpos=xpos,ypos=ypos)) xy = XY().rename(x='xpos', y='ypos') xy.add_subscriber(subscriber) xy.event(x=4,y=50) xy.clear() xy.event(x=0,y=0)