diff --git a/holoviews/plotting/mpl/__init__.py b/holoviews/plotting/mpl/__init__.py index 12974b462c..ece855f26f 100644 --- a/holoviews/plotting/mpl/__init__.py +++ b/holoviews/plotting/mpl/__init__.py @@ -1,12 +1,3 @@ - -try: - # Switching to 'agg' backend (may be overridden in holoviews.rc) - import matplotlib.pyplot as plt - plt.switch_backend('agg') -except: - pass - - import os from distutils.version import LooseVersion diff --git a/holoviews/plotting/mpl/plot.py b/holoviews/plotting/mpl/plot.py index 10199034d4..97554273b7 100644 --- a/holoviews/plotting/mpl/plot.py +++ b/holoviews/plotting/mpl/plot.py @@ -119,6 +119,12 @@ def __init__(self, fig=None, axis=None, **params): if self.fig_latex: self.fig_rcparams['text.usetex'] = True + if self.renderer.interactive: + plt.ion() + self._close_figures = False + else: + plt.ioff() + with mpl.rc_context(rc=self.fig_rcparams): fig, axis = self._init_axis(fig, axis) diff --git a/holoviews/plotting/mpl/renderer.py b/holoviews/plotting/mpl/renderer.py index 424b993eed..e9f80cd1a8 100644 --- a/holoviews/plotting/mpl/renderer.py +++ b/holoviews/plotting/mpl/renderer.py @@ -53,12 +53,15 @@ class MPLRenderer(Renderer): Output render multi-frame (typically animated) format. If None, no multi-frame rendering will occur.""") + interactive = param.Boolean(default=False, doc=""" + Whether to enable interactive plotting allowing interactive + plotting with explicitly calling show.""") + mode = param.ObjectSelector(default='default', objects=['default', 'mpld3', 'nbagg'], doc=""" The 'mpld3' mode uses the mpld3 library whereas the 'nbagg' uses matplotlib's the experimental nbagg backend. """) - # : (animation writer, format, anim_kwargs, extra_args) ANIMATION_OPTS = { 'webm': ('ffmpeg', 'webm', {}, @@ -112,6 +115,31 @@ def __call__(self, obj, fmt='auto'): 'mime_type':MIME_TYPES[fmt]} + def show(self, obj): + """ + Renders the supplied object and displays it using the active + GUI backend. + """ + if self.interactive: + if isinstance(obj, list): + return [self.get_plot(o) for o in obj] + return self.get_plot(obj) + + from .plot import MPLPlot + MPLPlot._close_figures = False + try: + plots = [] + objects = obj if isinstance(obj, list) else [obj] + for o in obj: + plots.append(self.get_plot(o)) + plt.show() + except: + MPLPlot._close_figures = True + raise + MPLPlot._close_figures = True + return plots[0] if len(plots) == 1 else plots + + @classmethod def plot_options(cls, obj, percent_size): """ @@ -279,3 +307,11 @@ def validate(cls, options): "matplotlib:nbagg.\nSwitching widget mode to 'live'.") options['widgets'] = 'live' return options + + @classmethod + def load_nb(cls, inline=True): + """ + Initialize matplotlib backend + """ + import matplotlib.pyplot as plt + plt.switch_backend('agg')