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

Added custom rcParam context manager #1300

Merged
merged 2 commits into from
Apr 17, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid confusion maybe this should be _rc_context if we expect to use the decorator and not this context manager directly. Perhaps you could even inline this context manager inside mpl_rc_context?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inlining seems inefficient but I'll happily rename.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is at all inefficient to inline - decorators are only applied once at definition time.

"""
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