同步选择> 1个图表

时间:2019-06-19 19:13:22

标签: holoviews pyviz hvplot

如何同步下面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)

1 个答案:

答案 0 :(得分:2)

链接的选择很简单:

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上建模)