Search code examples
pythonholoviews

Change holoviews extension on a single plot


I usually use the bokeh backend, but I want to be able to plot surfaces as well, as supported by the matplotlib backend but not by bokeh. Is there a way that I can designate that a particular plot should be rendered using a different backend without changing the backend used for subsequent plots? I could do the following e.g. in a Jupyter notebook:

import holoviews as hv
hv.extension("bokeh")

# plots involving bokeh backend
surface = hv.Surface(np.sin(np.linspace(0,100*np.pi*2,10000)).reshape(100,100))
hv.extension("matplotlib")
surface
hv.extension("bokeh")
# more plots involving bokeh

But it's rather inconvenient. Is there anything that I could do like

surface = surface.opts(extension="matplotlib")

so that this element will be rendered with matplotlib but rendering other plots will be unaffected?


Solution

  • If you're using holoviews >=1.11.0 you can use the hv.output utility. First make sure to load both backends:

    import holoviews as hv
    hv.extension("bokeh", "matplotlib")
    ... # Various plots using bokeh
    

    Now you can use the output utility to display a single plot using matplotlib (or plotly):

    surface = hv.Surface(np.sin(np.linspace(0,100*np.pi*2,10000)).reshape(100,100))
    hv.output(surface, backend='matplotlib')
    

    If you also have to provide custom options for the different backend you can specify the backend as part of the .opts (or .options declaration):

    hv.output(surface.opts(fig_size=200, backend='matplotlib'), backend='matplotlib')