Better handling of matplotlib rc settings.

This commit is contained in:
Lucas Verney 2016-03-02 14:24:09 +01:00
parent fa104cf48f
commit b59cf7aacc
3 changed files with 56 additions and 36 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,6 +2,8 @@
The :mod:`replot` module is a (sane) Python plotting module, abstracting on top The :mod:`replot` module is a (sane) Python plotting module, abstracting on top
of Matplotlib. of Matplotlib.
""" """
import shutil
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import seaborn.apionly as sns import seaborn.apionly as sns
@ -17,9 +19,23 @@ DEFAULT_X_INTERVAL = np.linspace(-10, 10,
DEFAULT_NB_SAMPLES) DEFAULT_NB_SAMPLES)
# TODO: Remove it, this is interfering with matplotlib def mpl_custom_rc_context():
plt.rcParams['figure.figsize'] = (10.0, 8.0) # Larger figures by default """
plt.rcParams['text.usetex'] = True # Use LaTeX rendering 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(): class Figure():
@ -69,29 +85,32 @@ class Figure():
""" """
Actually render and show the :class:`Figure` object. Actually render and show the :class:`Figure` object.
""" """
# Tweak matplotlib to use seaborn # Use custom matplotlib context
sns.set() with mpl_custom_rc_context():
# Plot using specified color palette # Tweak matplotlib to use seaborn
with sns.color_palette(palette=self.palette, n_colors=self.max_colors): sns.set()
# Create figure # Plot using specified color palette
figure, axes = plt.subplots() with sns.color_palette(palette=self.palette,
# Add plots n_colors=self.max_colors):
for plot_ in self.plots: # Create figure
tmp_plots = axes.plot(*(plot_[0]), **(plot_[1])) figure, axes = plt.subplots()
# Do not clip line at the axes boundaries to prevent extremas # Add plots
# from being cropped. for plot_ in self.plots:
for tmp_plot in tmp_plots: tmp_plots = axes.plot(*(plot_[0]), **(plot_[1]))
tmp_plot.set_clip_on(False) # Do not clip line at the axes boundaries to prevent
# Set properties # extremas from being cropped.
axes.set_xlabel(self.xlabel) for tmp_plot in tmp_plots:
axes.set_ylabel(self.ylabel) tmp_plot.set_clip_on(False)
axes.set_title(self.title) # Set properties
self._legend(axes) axes.set_xlabel(self.xlabel)
# Draw figure axes.set_ylabel(self.ylabel)
figure.show() axes.set_title(self.title)
# Do not forget to restore matplotlib state, in order not to interfere self._legend(axes)
# with it. # Draw figure
sns.reset_orig() figure.show()
# Do not forget to restore matplotlib state, in order not to
# interfere with it.
sns.reset_orig()
def plot(self, *args, **kwargs): def plot(self, *args, **kwargs):
""" """

View File

@ -1,2 +1,3 @@
matplotlib>=1.5.1 matplotlib>=1.5.1
numpy>=1.10.4
seaborn>=0.7.0 seaborn>=0.7.0