The beauty of holoviews is that it allows you to choose between the modern browser-based bokeh and the good-old familiar matplotlib to display its plots (as well as plotly to some extend, mainly for 3D plots).
Spyder is able to render matplotlib plots either inline (i.e. in the python console itself or since recently is their new plots panel) or interactively (i.e. in a pop up window - several backends exist, amongst all qt). You can switch between these by typing %matplotlib inline
or %matplotlib qt
in your spyder ipython console.
These backends will then be the place where your holoview-generated matplotlib plots land!
Now, you need to explicitly tell holoviews to use matplotlib as a backend to render plots (what I refer to below as holoview_object can be either what they call an 'element' or a combination of these: layout, overlay, holomap...). You can do it using
matplotlib_fig = holoviews.render(holoview_object, backend='matplotlib')
and then create an empty matplotlib figure and hack its manager to display it in your default matplotlib backend:
dummy = plt.figure()
new_manager = dummy.canvas.manager
new_manager.canvas.figure = matplotlib_fig
fig.set_canvas(new_manager.canvas)
Using the concepts above, I made myself some utility functions to easily display matplotlib or bokeh plots from within spyder, directly or starting with a holoviews object, feel free to use them:
import matplotlib.pyplot as plt
import bokeh as bk
import holoviews as hv
def mplshow(fig):
# create a dummy figure and use its
# manager to display "fig"
dummy = plt.figure()
new_manager = dummy.canvas.manager
new_manager.canvas.figure = fig
fig.set_canvas(new_manager.canvas)
def bkshow(bkfig, title=None, save=0, savePath='~/Downloads'):
if title is None: title=bkfig.__repr__()
if save:bk.plotting.output_file(f'{title}.html')
bk.plotting.show(bkfig)
def hvshow(hvobject, backend='matplotlib', return_mpl=True):
'''
Holoview utility which
- for dynamic display, interaction and data exploration:
in browser, pops up a holoview object as a bokeh figure
- for static instanciation, refinement and data exploitation:
in matplotlib current backend, pops up a holoview object as a matplotlib figure
and eventually returns it for further tweaking.
Parameters:
- hvobject: a Holoviews object e.g. Element, Overlay or Layout.
- backend: 'bokeh' or 'matplotlib', which backend to use to show figure
- return_mpl: bool, returns a matplotlib figure
'''
assert backend in ['bokeh', 'matplotlib']
if backend=='matplotlib' or return_mpl:
mplfig=hv.render(hvobject, backend='matplotlib')
if backend=='bokeh': bkshow(hv.render(hvobject, backend='bokeh'))
elif backend=='matplotlib': mplshow(mplfig)
if return_mpl: return mplfig
In summary:
if you wish to render your plot statically in the spyder plot pane (or the python console if you do not use their plot pane), do:
%matplotlib inline
hvshow(holoviews_object, 'matplotlib')
if you wish to pop up your plot in an interactive qt window, do:
%matplotlib qt
hvshow(holoviews_object, 'matplotlib')
if you wish to pop up your plot in your browser (i.e. with bokeh), also interactively, do:
hvshow(holoviews_object, 'bokeh')
I love spyder (much more than jupyter notebooks) as much as holoviews and I am thrilled to be able to use both together!