Skip to content

Commit 15afff5

Browse files
authored
Merge pull request #86 from neutrinoceros/future_mpl_compat
ENH: work around pending deprecation warnings in MPL
2 parents f888aa3 + 6e09998 commit 15afff5

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

cmocean/cm.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
import numpy as np
1414
import os
15-
from matplotlib import colors, cm
16-
15+
from matplotlib import colors
16+
import matplotlib as mpl
17+
from packaging.version import Version
18+
from importlib.metadata import version
1719
from . import tools
1820

1921
# Location of rgb files
@@ -27,6 +29,18 @@
2729
# initialize dictionary to contain colormaps
2830
cmap_d = dict()
2931

32+
33+
# a comparable version object
34+
MPL_VERSION = Version(version("matplotlib"))
35+
36+
def _register_cmap(cmap, *, name):
37+
# wrap matplotlib.cm API, use non-deprecated API when available
38+
if MPL_VERSION >= Version("3.5"):
39+
mpl.colormaps.register(cmap, name=name)
40+
else:
41+
# deprecated API
42+
mpl.cm.register_cmap(name=name, cmap=cmap)
43+
3044
# add colormaps and reversed to dictionary
3145
for cmapname in cmapnames:
3246
rgb = np.loadtxt(os.path.join(datadir, cmapname + '-rgb.txt'))
@@ -39,12 +53,12 @@
3953
rgb_with_alpha = np.zeros((rgb.shape[0],4))
4054
rgb_with_alpha[:,:3] = rgb
4155
rgb_with_alpha[:,3] = 1. #set alpha channel to 1
42-
reg_map = colors.ListedColormap(rgb_with_alpha, 'cmo.' + cmapname, rgb.shape[0])
43-
cm.register_cmap(cmap = reg_map)
56+
reg_map = colors.ListedColormap(rgb_with_alpha, N=rgb.shape[0])
57+
_register_cmap(reg_map, name=f'cmo.{cmapname}')
4458

4559
# Register the reversed map
46-
reg_map_r = colors.ListedColormap(rgb_with_alpha[::-1,:], 'cmo.' + cmapname + '_r', rgb.shape[0])
47-
cm.register_cmap(cmap = reg_map_r)
60+
reg_map_r = colors.ListedColormap(rgb_with_alpha[::-1,:], N=rgb.shape[0])
61+
_register_cmap(reg_map_r, name=f'cmo.{cmapname}_r')
4862

4963
# make colormaps available to call
5064
locals().update(cmap_d)

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def run_tests(self):
4545
scripts = [],
4646
keywords = ['colormaps', 'oceanography', 'plotting', 'visualization'],
4747
setup_requires=['setuptools'],
48-
install_requires=['matplotlib', 'numpy'],
48+
install_requires=['matplotlib', 'numpy', 'packaging'],
4949
tests_require=['pytest'],
50+
python_requires=">=3.8",
5051
extras_require=extras_require
5152
)

0 commit comments

Comments
 (0)