同步选择 > 1 个图表

synchronize selection of > 1 chart

如何在下面的 2 个图表中同步选择?另外,如何获取框选择的边界?

import holoviews as hv
import hvplot.pandas
from bokeh.sampledata.autompg import autompg
hv.extension('bokeh')

hv.Layout([autompg.hvplot.scatter(x='mpg', y='yr', tools=['box_select']), autompg.hvplot.scatter(x='mpg', y='yr', tools=['box_select'])]).cols(1)

链接选择很简单:

import holoviews as hv
import hvplot.pandas
from bokeh.sampledata.autompg import autompg
hv.extension('bokeh')

from holoviews.plotting.links import DataLink
a = autompg.hvplot.scatter(x='mpg', y='yr', tools=['box_select'])
b = autompg.hvplot.scatter(x='mpg', y='yr', tools=['box_select'])
DataLink(a, b)
hv.Layout([a, b]).cols(1)

文档:https://www.holoviews.org/user_guide/Linking_Plots.html

现在检索边界。您可以为此使用 BoundsXY:

import numpy as np
import holoviews as hv
from holoviews.streams import BoundsXY

data = np.random.multivariate_normal((0, 0), [[1, 0.1], [0.1, 1]], (1000,))
points = hv.Points(data).opts(tools=['box_select'])
sel = BoundsXY(source=points)
def cog(bounds):
    'Center of gravity'
    if bounds is None:
        bounds=(0, 0, 0, 0)
    index = points.dframe().x.between(*bounds[::2]) & points.dframe().y.between(*bounds[1::2])
    x = points.dframe().loc[index, 'x'].mean() if index.any() else []
    y = points.dframe().loc[index, 'y'].mean() if index.any() else []
    return hv.Points((x, y)).opts(size=10)

mean_sel = hv.DynamicMap(cog, kdims=[], streams=[sel])

points * mean_sel

(仿照 http://holoviews.org/reference/apps/bokeh/selection_stream.html