import numpy as np import holoviews as hv from holoviews import opts hv.extension('bokeh') from holoviews.plotting.links import DataLink scatter1 = hv.Scatter(np.arange(100)) scatter2 = hv.Scatter(np.arange(100)[::-1], 'x2', 'y2') dlink = DataLink(scatter1, scatter2) (scatter1 + scatter2).opts( opts.Scatter(tools=['box_select', 'lasso_select'])) dlink.unlink() (scatter1 + scatter2) from holoviews.plotting.links import RangeToolLink data = np.random.randn(1000).cumsum() source = hv.Curve(data).opts(width=800, height=125, axiswise=True, default_tools=[]) target = hv.Curve(data).opts(width=800, labelled=['y'], toolbar=None) rtlink = RangeToolLink(source, target) (target + source).opts(merge_tools=False).cols(1) import param from holoviews.plotting.links import Link class MeanLineLink(Link): column = param.String(default='x', doc=""" The column to compute the mean on.""") _requires_target = True renderer = hv.renderer('bokeh') plot = renderer.get_plot(hv.Scatter([])) plot.handles.keys() plot = renderer.get_plot(hv.HLine(0)) plot.handles.keys() from holoviews.plotting.bokeh import LinkCallback class MeanLineCallback(LinkCallback): source_model = 'selected' source_handles = ['cds'] on_source_changes = ['indices'] target_model = 'glyph' source_code = """ var inds = source_selected.indices var d = source_cds.data var vm = 0 if (inds.length == 0) return for (var i = 0; i < inds.length; i++) vm += d[column][inds[i]] vm /= inds.length target_glyph.location = vm """ def validate(self): assert self.link.column in self.source_plot.handles['cds'].data MeanLineLink.register_callback('bokeh', MeanLineCallback) options = opts.Scatter( selection_fill_color='firebrick', alpha=0.4, line_color='black', size=8, tools=['lasso_select', 'box_select'], width=500, height=500, active_tools=['lasso_select'] ) scatter = hv.Scatter(np.random.randn(500, 2)).opts(options) vline = hv.VLine(scatter['x'].mean()).opts(color='black') hline = hv.HLine(scatter['y'].mean()).opts(color='black') MeanLineLink(scatter, vline, column='x') MeanLineLink(scatter, hline, column='y') scatter * hline * vline