Skip to content

Commit

Permalink
Fix for importing mpl colormaps with recent versions of matplotlib (#…
Browse files Browse the repository at this point in the history
…1077)

* Fix for importing mpl colormaps with recent versions of matplotlib
* Remove support for matplotlib < 3.4
* bump matplotlib recommended version
* Remove code for ignoring MatplotlibDeprecationWarning
  • Loading branch information
ejeschke authored Dec 4, 2023
1 parent f0eecdf commit 646a68f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
1 change: 1 addition & 0 deletions doc/WhatsNew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Ver 5.0.0 (unreleased)
- Fix for a logger annoyance message when mousing over far edge of image
- Updates for deprecations in numpy 2.0
- Fix for missing menubar on some versions of Qt and Mac OS X
- Fix for importing mpl colormaps with recent versions of matplotlib

Ver 4.1.0 (2022-06-30)
======================
Expand Down
22 changes: 4 additions & 18 deletions ginga/cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
import warnings

import numpy as np
from astropy.utils import minversion

__all__ = ['ColorMap', 'add_cmap', 'get_cmap', 'has_cmap', 'get_names',
'matplotlib_to_ginga_cmap', 'add_matplotlib_cmap',
'add_matplotlib_cmaps']

MPL_LT_3_4 = not minversion('matplotlib', '3.4')


# Some built in colormaps

cmap_soss = (
Expand Down Expand Up @@ -13309,28 +13303,20 @@ def add_matplotlib_cmap(cm, name=None):
def add_matplotlib_cmaps(fail_on_import_error=True):
"""Add all matplotlib colormaps."""
try:
import matplotlib.pyplot as plt
from matplotlib import cm as _cm
if MPL_LT_3_4:
from matplotlib.cbook import mplDeprecation as MatplotlibDeprecationWarning
else:
from matplotlib import MatplotlibDeprecationWarning
from matplotlib import colormaps as _mpl_cm
except ImportError:
if fail_on_import_error:
raise
# silently fail
return

# NOTE: Update if matplotlib has new public API for this.
for name in plt.colormaps():
for name in _mpl_cm:
if not isinstance(name, str):
continue
try:
# Do not load deprecated colormaps
with warnings.catch_warnings():
warnings.simplefilter('error', MatplotlibDeprecationWarning)
cm = _cm.get_cmap(name)
add_matplotlib_cmap(cm, name=name)
cm = _mpl_cm[name]
add_matplotlib_cmap(cm, name=name)
except Exception as e:
if fail_on_import_error:
print(f"Error adding colormap '{name}': {e!r}")
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ setup_requires = setuptools_scm
recommended =
python-magic>=0.4.15
scipy>=0.18.1
matplotlib>=2.1
matplotlib>=3.4
opencv-python>=4.5.4.58
exifread>=2.3.2
beautifulsoup4>=4.3.2
Expand Down

0 comments on commit 646a68f

Please sign in to comment.