From bf39e10b36dcd30c542f6add0c8575816b721cf8 Mon Sep 17 00:00:00 2001
From: Philipp Rudiger
Date: Mon, 17 Apr 2017 11:51:44 +0100
Subject: [PATCH 1/2] Added custom rcParam context manager
---
holoviews/plotting/mpl/element.py | 2 ++
holoviews/plotting/mpl/pandas.py | 3 ++-
holoviews/plotting/mpl/plot.py | 20 ++++++++++++++++++--
holoviews/plotting/mpl/raster.py | 2 +-
4 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/holoviews/plotting/mpl/element.py b/holoviews/plotting/mpl/element.py
index 08e126f995..8b1124d1dc 100644
--- a/holoviews/plotting/mpl/element.py
+++ b/holoviews/plotting/mpl/element.py
@@ -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
@@ -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:
diff --git a/holoviews/plotting/mpl/pandas.py b/holoviews/plotting/mpl/pandas.py
index 21f7947a35..4ab96e7ef2 100644
--- a/holoviews/plotting/mpl/pandas.py
+++ b/holoviews/plotting/mpl/pandas.py
@@ -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):
@@ -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)
diff --git a/holoviews/plotting/mpl/plot.py b/holoviews/plotting/mpl/plot.py
index 06d65bfa13..bbe3837d76 100644
--- a/holoviews/plotting/mpl/plot.py
+++ b/holoviews/plotting/mpl/plot.py
@@ -1,6 +1,7 @@
from __future__ import division
from itertools import chain
+from contextlib import contextmanager
import numpy as np
import matplotlib as mpl
@@ -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
diff --git a/holoviews/plotting/mpl/raster.py b/holoviews/plotting/mpl/raster.py
index 9f840b84d9..7b22b78284 100644
--- a/holoviews/plotting/mpl/raster.py
+++ b/holoviews/plotting/mpl/raster.py
@@ -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)
From b4e4f6755d031fca38193442623c11bf4d5a6647 Mon Sep 17 00:00:00 2001
From: Philipp Rudiger
Date: Mon, 17 Apr 2017 12:07:19 +0100
Subject: [PATCH 2/2] Made the custom _rc_context context manager hidden
---
holoviews/plotting/mpl/plot.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/holoviews/plotting/mpl/plot.py b/holoviews/plotting/mpl/plot.py
index bbe3837d76..333b973874 100644
--- a/holoviews/plotting/mpl/plot.py
+++ b/holoviews/plotting/mpl/plot.py
@@ -21,7 +21,7 @@
@contextmanager
-def rc_context(rcparams):
+def _rc_context(rcparams):
"""
Context manager that temporarily overrides the pyplot rcParams.
"""
@@ -40,7 +40,7 @@ def mpl_rc_context(f):
in the plots fig_rcparams while when method is called.
"""
def wrapper(self, *args, **kwargs):
- with rc_context(self.fig_rcparams):
+ with _rc_context(self.fig_rcparams):
return f(self, *args, **kwargs)
return wrapper