Unable to set xlim for image using apply.opts

I am reading through the user guide on Responding to Event[http://holoviews.org/user_guide/Responding_to_Events.html] and trying to modify one of the examples to my needs.

Here is the minimal code:

import holoviews as hv
from holoviews import opts
from holoviews.streams import Stream
import param
import numpy as np

hv.extension('bokeh')

ls = np.linspace(0, 10, 200)
xx, yy = np.meshgrid(ls, ls)

image = hv.Image(np.sin(xx)*np.cos(yy))

class Style(param.Parameterized):
    colormap = param.ObjectSelector(default='viridis', objects=['viridis', 'plasma', 'magma'])

    color_levels = param.Integer(default=255, bounds=(1, 255))
    
    xlim = param.Range(default=(0,1), bounds=(-10,10))   

style = Style()
image.apply.opts(colorbar=True, width=400, cmap=style.param.colormap, color_levels=style.param.color_levels, xlim=style.param.xlim)

This creates as 2D plot.
If I do style.color_levels= 13 then the color levels change, but if I do style.xlim = (-0.5, -.1) the xlim does not change (even though the style.xlim now has a new value).

Is this a bug or am I doing something wrong?

style.xlim.value Maybe

That does not work - neither style.param.xlim.value nor style.xlim.value. Plus, why should I have to add .value for this when .value was not needed for the others like color_levels?

@dhruvbalwada based on the documentation you linked to (the ’ Using .apply.opts') section I would expect what you’re doing to work. So I think it would be safe to open an issue describing the bug (or need for some additional documentation) here https://github.com/holoviz/holoviews . In the meantime, you can accomplish what you’re going for with this slightly more explicit approach:

class Style(param.Parameterized):
    colormap = param.ObjectSelector(default='viridis', objects=['viridis', 'plasma', 'magma'])
    color_levels = param.Integer(default=255, bounds=(1, 255))
    xlim = param.Range(default=(0,1), bounds=(-10,10))   

    @param.depends('colormap','color_levels','xlim')
    def view(self):
        image = hv.Image(np.sin(xx)*np.cos(yy))
        return image.opts(colorbar=True, width=400, 
                       cmap=self.colormap, 
                       color_levels=self.color_levels, 
                       xlim=self.xlim,
                      )

myplot = Style()
pn.panel(myplot.view)

Then you can change the x-axis limits programmatically via myplot.xlim = (-1, 0.5)

1 Like

Thanks Scott. This is a good work around, but still a bug relative to the documentation about apply.opts. I will open an issue.

Panel will not update ranges unless framewise=True is set. While this is surprising it is technically not a bug. That said it is arguable that xlim should always be applied.