Skip to content

Commit

Permalink
Merge pull request #1214 from ioam/mpl_interactive
Browse files Browse the repository at this point in the history
Allow working with Renderers interactively outside the notebook
  • Loading branch information
jlstevens authored Apr 8, 2017
2 parents 6a1b38e + 2d42359 commit 193aefc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
9 changes: 0 additions & 9 deletions holoviews/plotting/mpl/__init__.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 6 additions & 0 deletions holoviews/plotting/mpl/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
38 changes: 37 additions & 1 deletion holoviews/plotting/mpl/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """)


# <format name> : (animation writer, format, anim_kwargs, extra_args)
ANIMATION_OPTS = {
'webm': ('ffmpeg', 'webm', {},
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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')

0 comments on commit 193aefc

Please sign in to comment.