Skip to content

Commit

Permalink
Merge pull request #1300 from ioam/mpl_rccontext
Browse files Browse the repository at this point in the history
Added custom rcParam context manager
  • Loading branch information
jlstevens authored Apr 17, 2017
2 parents 1b94bfd + b4e4f67 commit 078de8b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions holoviews/plotting/mpl/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ def _set_axis_ticks(self, axis, ticks, log=False, rotation=0):
tick.set_rotation(rotation)


@mpl_rc_context
def update_frame(self, key, ranges=None, element=None):
"""
Set the plot(s) to the given frame number. Operates by
Expand Down Expand Up @@ -838,6 +839,7 @@ def initialize_plot(self, ranges=None):
title=self._format_title(key))


@mpl_rc_context
def update_frame(self, key, ranges=None, element=None):
axis = self.handles['axis']
if element is None:
Expand Down
3 changes: 2 additions & 1 deletion holoviews/plotting/mpl/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ...core.options import Store
from ...interface.pandas import DFrame, DataFrameView, pd
from .element import ElementPlot
from .plot import mpl_rc_context


class DFrameViewPlot(ElementPlot):
Expand Down Expand Up @@ -65,7 +66,7 @@ def __init__(self, view, **params):
if self.hmap.last.plot_type and 'plot_type' not in params:
self.plot_type = self.hmap.last.plot_type


@mpl_rc_context
def initialize_plot(self, ranges=None):
element = self.hmap.last
self._validate(element)
Expand Down
20 changes: 18 additions & 2 deletions holoviews/plotting/mpl/plot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import division

from itertools import chain
from contextlib import contextmanager

import numpy as np
import matplotlib as mpl
Expand All @@ -19,12 +20,27 @@
from .util import compute_ratios, fix_aspect


@contextmanager
def _rc_context(rcparams):
"""
Context manager that temporarily overrides the pyplot rcParams.
"""
old_rcparams = plt.rcParams.copy()
plt.rcParams.update(rcparams)
try:
yield
except:
pass
finally:
plt.rcParams = old_rcparams

def mpl_rc_context(f):
"""
Applies matplotlib rc params while when method is called.
Decorator for MPLPlot methods applying the matplotlib rc params
in the plots fig_rcparams while when method is called.
"""
def wrapper(self, *args, **kwargs):
with mpl.rc_context(rc=self.fig_rcparams):
with _rc_context(self.fig_rcparams):
return f(self, *args, **kwargs)
return wrapper

Expand Down
2 changes: 1 addition & 1 deletion holoviews/plotting/mpl/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def initialize_plot(self, ranges=None):
kwargs = self._get_axis_kwargs()
return self._finalize_axis(key, ranges=ranges, **kwargs)


@mpl_rc_context
def update_frame(self, key, ranges=None):
grid = self._get_frame(key)
ranges = self.compute_ranges(self.layout, key, ranges)
Expand Down

0 comments on commit 078de8b

Please sign in to comment.