Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple targets for RangeToolLink #6464

Open
MatusGasparik opened this issue Dec 4, 2024 · 0 comments
Open

Allow multiple targets for RangeToolLink #6464

MatusGasparik opened this issue Dec 4, 2024 · 0 comments

Comments

@MatusGasparik
Copy link

Is your feature request related to a problem? Please describe.

Holoviews has the RangeToolLink which can be used to link the axes between the source and the target element:

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)

It is often the case that one needs to link multiple targets to the range tool, but in holoviews this is currently not possible:

data = np.random.randn(1000).cumsum()
x1 = np.arange(data.size)
x2 = x1 * 0.5

source = hv.Curve((x1, data)).opts(width=800, height=125, axiswise=True, default_tools=[])
target1 = hv.Curve((x1, data)).opts(width=800, height=200, labelled=['y'], toolbar=None)
target2 = hv.Area((x2, data)).opts(width=800, height=200, labelled=['y'], toolbar=None)

rtlink = RangeToolLink(source, [target1, target2])  # ERROR: 

(target1 + target2 + source).opts(merge_tools=True, shared_axes=False).cols(1)

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[68], line 11
      8 target1 = hv.Curve((x1, data)).opts(width=800, height=200, labelled=['y'], toolbar=None)
      9 target2 = hv.Area((x2, data)).opts(width=800, height=200, labelled=['y'], toolbar=None)
---> 11 rtlink = RangeToolLink(source, [target1, target2])
     13 (target1 + target2 + source).opts(merge_tools=True, shared_axes=False).cols(1)

File [~/Dropbox/FHNW/Projekte/CoGE/wt-coge-va/feature_gam/.pixi/envs/jupyter/lib/python3.11/site-packages/holoviews/plotting/links.py:44](http://localhost:8888/lab/workspaces/auto-g/tree/docs/~/Dropbox/FHNW/Projekte/CoGE/wt-coge-va/feature_gam/.pixi/envs/jupyter/lib/python3.11/site-packages/holoviews/plotting/links.py#line=43), in Link.__init__(self, source, target, **params)
     42 # Source is stored as a weakref to allow it to be garbage collected
     43 self._source = None if source is None else weakref.ref(source)
---> 44 self._target = None if target is None else weakref.ref(target)
     45 super().__init__(**params)
     46 self.link()

TypeError: cannot create weak reference to 'list' object
Selection deleted

Describe the solution you'd like

It would be nice to be able to pass a list of targets to the RangeToolLink:

rtlink = RangeToolLink(source, [target1, target2]) 

At the moment it is possbile to achive this functionality in a somewhat hacky implicit way using shared_axes=True and manipulating the kdims - see #6463. So the role of the shared_axes parameter should be clarified.

Describe alternatives you've considered

In Bokeh, this could look like so:

from bokeh.plotting import figure, show
from bokeh.models import RangeTool, Range1d, ColumnDataSource
from bokeh.layouts import column
import numpy as np


y = np.random.randn(1000).cumsum()
x = np.arange(y.size)
y_area = y - y.mean()

source = ColumnDataSource(data=dict(x=x, y=y, y_area=y_area))

overview = figure(
    height=120,
    width=800,
    tools="",
    toolbar_location=None,
    x_range=(x.min(), x.max()),
    y_axis_location="right",
)
overview.line('x', 'y', source=source, line_width=2, color="black")
overview.plot.title = "Overview"

detail1 = figure(
    height=200,
    width=800,
    tools="xpan", toolbar_location=None, 
)
detail1.varea('x', 0, 'y_area', source=source, fill_color="grey", alpha=0.4)
detail1.line(x, y_area, line_width=1, color="black")
detail1.yaxis.axis_label = "y - mean(y)"

detail2 = figure(
    height=200,
    width=800,
    tools="xpan", toolbar_location=None,
)
detail2.line('x', 'y', source=source, line_width=2, color="blue")
detail2.yaxis.axis_label = "y"

range_tool = RangeTool(x_range=Range1d(x[500], x[700]))
range_tool.overlay.fill_color = "navy"
range_tool.overlay.fill_alpha = 0.2
overview.add_tools(range_tool)

detail1.x_range = range_tool.x_range
detail2.x_range = range_tool.x_range

layout = column(overview, detail1, detail2, )
show(layout)

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant