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,17 +85,20 @@ class Figure():
""" """
Actually render and show the :class:`Figure` object. Actually render and show the :class:`Figure` object.
""" """
# Use custom matplotlib context
with mpl_custom_rc_context():
# Tweak matplotlib to use seaborn # Tweak matplotlib to use seaborn
sns.set() sns.set()
# Plot using specified color palette # Plot using specified color palette
with sns.color_palette(palette=self.palette, n_colors=self.max_colors): with sns.color_palette(palette=self.palette,
n_colors=self.max_colors):
# Create figure # Create figure
figure, axes = plt.subplots() figure, axes = plt.subplots()
# Add plots # Add plots
for plot_ in self.plots: for plot_ in self.plots:
tmp_plots = axes.plot(*(plot_[0]), **(plot_[1])) tmp_plots = axes.plot(*(plot_[0]), **(plot_[1]))
# Do not clip line at the axes boundaries to prevent extremas # Do not clip line at the axes boundaries to prevent
# from being cropped. # extremas from being cropped.
for tmp_plot in tmp_plots: for tmp_plot in tmp_plots:
tmp_plot.set_clip_on(False) tmp_plot.set_clip_on(False)
# Set properties # Set properties
@ -89,8 +108,8 @@ class Figure():
self._legend(axes) self._legend(axes)
# Draw figure # Draw figure
figure.show() figure.show()
# Do not forget to restore matplotlib state, in order not to interfere # Do not forget to restore matplotlib state, in order not to
# with it. # interfere with it.
sns.reset_orig() 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