Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow working with Renderers interactively outside the notebook #1214

Merged
merged 6 commits into from
Apr 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')