[yt-svn] commit/yt: ngoldbaum: ensure PlotWindow plots continue to look the same under matplotlib 2.0

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Jun 15 10:55:03 PDT 2016


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/f19d649bfe7a/
Changeset:   f19d649bfe7a
Branch:      yt
User:        ngoldbaum
Date:        2016-06-01 17:25:41+00:00
Summary:     ensure PlotWindow plots continue to look the same under matplotlib 2.0

This means that PlotWindow-style plots (SlicePlot, ProjectionPlot, off-axis
variants, PhasePlot, but not ProfilePlot) continue to look the same under
matplotlib 2.0.

ProfilePlot will come separately since I'd like to make it look more similar to
the other plotting classes.
Affected #:  3 files

diff -r 00a06eca12a1752a1026d4b93163568371ae9351 -r f19d649bfe7a6c30793532a99ef17decf9034e5a yt/funcs.py
--- a/yt/funcs.py
+++ b/yt/funcs.py
@@ -963,3 +963,24 @@
     except ImportError:
         requests = None
     return requests
+
+ at contextlib.contextmanager
+def dummy_context_manager(*args, **kwargs):
+    yield
+
+def matplotlib_style_context(style_name=None, after_reset=True):
+    """Returns a context manager for controlling matplotlib style.
+
+    Arguments are passed to matplotlib.style.context() if specified. Defaults
+    to setting "classic" style, after resetting to the default config parameters.
+
+    On older matplotlib versions (<=1.5.0) where matplotlib.style isn't
+    available, returns a dummy context manager.
+    """
+    if style_name is None:
+        style_name = 'classic'
+    try:
+        import matplotlib.style
+        return matplotlib.style.context(style_name, after_reset=after_reset)
+    except ImportError:
+        return dummy_context_manager()

diff -r 00a06eca12a1752a1026d4b93163568371ae9351 -r f19d649bfe7a6c30793532a99ef17decf9034e5a yt/visualization/base_plot_types.py
--- a/yt/visualization/base_plot_types.py
+++ b/yt/visualization/base_plot_types.py
@@ -19,7 +19,8 @@
     get_image_suffix, \
     mylog, \
     iterable, \
-    get_brewer_cmap
+    get_brewer_cmap, \
+    matplotlib_style_context
 import numpy as np
 
 
@@ -70,6 +71,9 @@
             axes.set_position(axrect)
             self.axes = axes
         self.canvas = FigureCanvasAgg(self.figure)
+        for which in ['major', 'minor']:
+            for axis in 'xy':
+                self.axes.tick_params(which=which, axis=axis, direction='in')
 
     def save(self, name, mpl_kwargs=None, canvas=None):
         """Choose backend and save image to disk"""
@@ -97,7 +101,8 @@
             mylog.warning("Unknown suffix %s, defaulting to Agg", suffix)
             canvas = self.canvas
 
-        canvas.print_figure(name, **mpl_kwargs)
+        with matplotlib_style_context():
+            canvas.print_figure(name, **mpl_kwargs)
         return name
 
     def _get_labels(self):
@@ -157,12 +162,15 @@
             self.cb.set_ticks(yticks)
         else:
             self.cb = self.figure.colorbar(self.image, self.cax)
+        for which in ['major', 'minor']:
+            self.cax.tick_params(which=which, axis='y', direction='in')
 
     def _repr_png_(self):
         from ._mpl_imports import FigureCanvasAgg
         canvas = FigureCanvasAgg(self.figure)
         f = BytesIO()
-        canvas.print_figure(f)
+        with matplotlib_style_context():
+            canvas.print_figure(f)
         f.seek(0)
         return f.read()
 

diff -r 00a06eca12a1752a1026d4b93163568371ae9351 -r f19d649bfe7a6c30793532a99ef17decf9034e5a yt/visualization/profile_plotter.py
--- a/yt/visualization/profile_plotter.py
+++ b/yt/visualization/profile_plotter.py
@@ -42,7 +42,8 @@
 from yt.funcs import \
     ensure_list, \
     get_image_suffix, \
-    get_ipython_api_version
+    get_ipython_api_version, \
+    matplotlib_style_context
 
 def get_canvas(name):
     from . import _mpl_imports as mpl
@@ -286,7 +287,9 @@
                 fns.append("%s%s" % (prefix, suffix))
             else:
                 fns.append("%s_1d-Profile_%s_%s%s" % (prefix, xfn, uid, suffix))
-            plot.save(fns[-1], mpl_kwargs=mpl_kwargs)
+            mylog.info("Saving %s", fns[-1])
+            with matplotlib_style_context():
+                plot.save(fns[-1], mpl_kwargs=mpl_kwargs)
         return fns
 
     @validate_plot
@@ -333,7 +336,8 @@
         for uid, fig in iters:
             canvas = mpl.FigureCanvasAgg(fig)
             f = BytesIO()
-            canvas.print_figure(f)
+            with matplotlib_style_context():
+                canvas.print_figure(f)
             f.seek(0)
             img = base64.b64encode(f.read()).decode()
             ret += r'<img style="max-width:100%%;max-height:100%%;" ' \

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the yt-svn mailing list