From b59cf7aaccaf5ef56e9587490ccb5a38be90b152 Mon Sep 17 00:00:00 2001 From: "Phyks (Lucas Verney)" Date: Wed, 2 Mar 2016 14:24:09 +0100 Subject: [PATCH] Better handling of matplotlib rc settings. --- Examples.ipynb | 20 ++++++------- replot/__init__.py | 71 +++++++++++++++++++++++++++++----------------- requirements.txt | 1 + 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/Examples.ipynb b/Examples.ipynb index 17917e8..496ee3b 100644 --- a/Examples.ipynb +++ b/Examples.ipynb @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -799,7 +799,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -1606,7 +1606,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": { "collapsed": false, "scrolled": false @@ -2379,7 +2379,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -2397,7 +2397,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -3169,7 +3169,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -3187,7 +3187,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": { "collapsed": false, "scrolled": false @@ -3960,7 +3960,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -5563,7 +5563,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -6335,7 +6335,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" diff --git a/replot/__init__.py b/replot/__init__.py index c46400d..a3c8207 100644 --- a/replot/__init__.py +++ b/replot/__init__.py @@ -2,6 +2,8 @@ The :mod:`replot` module is a (sane) Python plotting module, abstracting on top of Matplotlib. """ +import shutil + import matplotlib.pyplot as plt import numpy as np import seaborn.apionly as sns @@ -17,9 +19,23 @@ DEFAULT_X_INTERVAL = np.linspace(-10, 10, DEFAULT_NB_SAMPLES) -# TODO: Remove it, this is interfering with matplotlib -plt.rcParams['figure.figsize'] = (10.0, 8.0) # Larger figures by default -plt.rcParams['text.usetex'] = True # Use LaTeX rendering +def mpl_custom_rc_context(): + """ + Overload ``matplotlib.rcParams`` to enable advanced features if \ + available. In particular, use LaTeX if available. + + :returns: A ``matplotlib.rc_context`` object to use in a ``with`` \ + statement. + """ + custom_rc = {} + # Add LaTeX in rc if available + if(shutil.which("latex") is not None and + shutil.which("gs") is not None and + shutil.which("dvipng") is not None): + # LateX dependencies are all available + custom_rc["text.usetex"] = True + custom_rc["text.latex.unicode"] = True + return plt.rc_context(rc=custom_rc) class Figure(): @@ -69,29 +85,32 @@ class Figure(): """ Actually render and show the :class:`Figure` object. """ - # Tweak matplotlib to use seaborn - sns.set() - # Plot using specified color palette - with sns.color_palette(palette=self.palette, n_colors=self.max_colors): - # Create figure - figure, axes = plt.subplots() - # Add plots - for plot_ in self.plots: - tmp_plots = axes.plot(*(plot_[0]), **(plot_[1])) - # Do not clip line at the axes boundaries to prevent extremas - # from being cropped. - for tmp_plot in tmp_plots: - tmp_plot.set_clip_on(False) - # Set properties - axes.set_xlabel(self.xlabel) - axes.set_ylabel(self.ylabel) - axes.set_title(self.title) - self._legend(axes) - # Draw figure - figure.show() - # Do not forget to restore matplotlib state, in order not to interfere - # with it. - sns.reset_orig() + # Use custom matplotlib context + with mpl_custom_rc_context(): + # Tweak matplotlib to use seaborn + sns.set() + # Plot using specified color palette + with sns.color_palette(palette=self.palette, + n_colors=self.max_colors): + # Create figure + figure, axes = plt.subplots() + # Add plots + for plot_ in self.plots: + tmp_plots = axes.plot(*(plot_[0]), **(plot_[1])) + # Do not clip line at the axes boundaries to prevent + # extremas from being cropped. + for tmp_plot in tmp_plots: + tmp_plot.set_clip_on(False) + # Set properties + axes.set_xlabel(self.xlabel) + axes.set_ylabel(self.ylabel) + axes.set_title(self.title) + self._legend(axes) + # Draw figure + figure.show() + # Do not forget to restore matplotlib state, in order not to + # interfere with it. + sns.reset_orig() def plot(self, *args, **kwargs): """ diff --git a/requirements.txt b/requirements.txt index f17c374..bc1f4ef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ matplotlib>=1.5.1 +numpy>=1.10.4 seaborn>=0.7.0