Skip to content

Commit

Permalink
Fix compatibility with new versions of matplotlib
Browse files Browse the repository at this point in the history
Fixes #11, closes #19.
  • Loading branch information
dean0x7d committed Aug 29, 2020
1 parent 53b5544 commit 620e04c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
[#21](https://github.com/dean0x7d/pybinding/issues/21), and
[#23](https://github.com/dean0x7d/pybinding/issues/23).

* Fixed compatibility with new versions of `matplotlib`: import error
([#11](https://github.com/dean0x7d/pybinding/issues/11)) and various warnings.

* Dropped support for Python 3.5. You must have Python 3.6 or newer to install this version.


Expand Down
5 changes: 4 additions & 1 deletion docs/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import pathlib
import warnings


docs = pathlib.Path(__file__).parent
Expand All @@ -17,4 +18,6 @@ def test_docs(example_file):
"""Make sure all example files execute without error"""
filename = str(example_file)
with open(filename, "rb") as file:
exec(compile(file.read(), filename, "exec"), {})
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
exec(compile(file.read(), filename, "exec"), {})
19 changes: 17 additions & 2 deletions pybinding/pltutils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Collection of utility functions for matplotlib"""
import warnings
from contextlib import contextmanager, suppress

import numpy as np
Expand All @@ -12,6 +13,20 @@
'set_palette', 'use_style']


def _set_smart_bounds(spine, value):
"""Hold on to the deprecated `set_smart_bounds()` for a little while longer
`set_smart_bounds()` was deprecated in matplotlib 3.2 and will be removed in the future.
This compatibility function ensures that we can keep using the function (while it's still
there) without any deprecation warnings. And when it is removed, suppress `AttributeError`
to make it a no-op. It's purely aesthetic change to the plots, but it's nice to keep it
while it's there.
"""
with warnings.catch_warnings(), suppress(AttributeError):
warnings.simplefilter("ignore", mpl.MatplotlibDeprecationWarning)
spine.set_smart_bounds(value)


@contextmanager
def axes(ax):
"""A context manager that sets the active Axes instance to `ax`
Expand Down Expand Up @@ -76,7 +91,7 @@ def despine(trim=False):
ax.yaxis.set_major_locator(plt.MaxNLocator(nbins="auto", steps=[1, 2, 5, 10]))

for v, side in [('x', 'bottom'), ('y', 'left')]:
ax.spines[side].set_smart_bounds(True)
_set_smart_bounds(ax.spines[side], True)
ticks = getattr(ax, "get_{}ticks".format(v))()
vmin, vmax = getattr(ax, "get_{}lim".format(v))()
ticks = ticks[(ticks >= vmin) & (ticks <= vmax)]
Expand Down Expand Up @@ -105,7 +120,7 @@ def respine():
ax = plt.gca()
for side in ['top', 'right', 'bottom', 'left']:
ax.spines[side].set_visible(True)
ax.spines[side].set_smart_bounds(False)
_set_smart_bounds(ax.spines[side], False)
ax.xaxis.set_ticks_position('both')
ax.yaxis.set_ticks_position('both')

Expand Down
3 changes: 2 additions & 1 deletion pybinding/support/collections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from matplotlib.collections import Collection, allow_rasterization
from matplotlib.collections import Collection
from matplotlib.artist import allow_rasterization


# noinspection PyAbstractClass
Expand Down
13 changes: 9 additions & 4 deletions tests/utils/compare_figures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import shutil
import tempfile
import warnings
from contextlib import suppress

import matplotlib as mpl
Expand Down Expand Up @@ -50,12 +51,16 @@ def _enter_style(self, style=pltutils.pb_style):
self._original_rc = mpl.rcParams.copy()
self._original_units_registry = matplotlib.units.registry.copy()

matplotlib.style.use(style)
mpl.use('Agg', warn=False)
with warnings.catch_warnings():
warnings.simplefilter("ignore", matplotlib.MatplotlibDeprecationWarning)
matplotlib.style.use(style)
mpl.use('Agg')

def _exit_style(self):
mpl.rcParams.clear()
mpl.rcParams.update(self._original_rc)
with warnings.catch_warnings():
warnings.simplefilter("ignore", matplotlib.MatplotlibDeprecationWarning)
mpl.rcParams.clear()
mpl.rcParams.update(self._original_rc)
matplotlib.units.registry.clear()
matplotlib.units.registry.update(self._original_units_registry)

Expand Down

0 comments on commit 620e04c

Please sign in to comment.