Search code examples
pythonzoomingholoviews

How to restrict zoom min and max ranges in python holoviews?


I am using holoviews to display a RGB picture using the command below (stack[1] being the image I want to display):

pix = hv.RGB(stack[1]).opts(
    xaxis=None, 
    yaxis=None,
    tools=['wheel_zoom','pan'],
    default_tools=[],
    active_tools=['wheel_zoom','pan']
)

It displays fine but when I use the wheel zoom, I can zoom out way past the limits of my image. Is there a way to restrict the limits of my plot so it never goes past the boundaries of my data/image?


Solution

  • May be there is an option but you can use an hook for this

    import holoviews as hv
    hv.extension('bokeh')
    import numpy as np
    from functools import partial
    def bounds_hook(plot, elem, xbounds=None, ybounds=None):
        x_range = plot.handles['plot'].x_range
        y_range = plot.handles['plot'].y_range
        if xbounds is not None:
            x_range.bounds = xbounds
        else:
            x_range.bounds = x_range.start, x_range.end 
        if ybounds is not None:
            y_range.bounds = ybounds
        else:
            y_range.bounds = y_range.start, y_range.end 
    (hv.Image(np.random.rand(100,100)).opts(hooks=[bounds_hook]) +
    hv.Image(np.random.rand(100,100)).opts(hooks=[partial(bounds_hook, xbounds=(-1, 1), ybounds=(-1,1))], axiswise=True))