|
12 | 12 |
|
13 | 13 | import numpy as np
|
14 | 14 | 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 |
17 | 19 | from . import tools
|
18 | 20 |
|
19 | 21 | # Location of rgb files
|
|
27 | 29 | # initialize dictionary to contain colormaps
|
28 | 30 | cmap_d = dict()
|
29 | 31 |
|
| 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 | + |
30 | 44 | # add colormaps and reversed to dictionary
|
31 | 45 | for cmapname in cmapnames:
|
32 | 46 | rgb = np.loadtxt(os.path.join(datadir, cmapname + '-rgb.txt'))
|
|
39 | 53 | rgb_with_alpha = np.zeros((rgb.shape[0],4))
|
40 | 54 | rgb_with_alpha[:,:3] = rgb
|
41 | 55 | 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}') |
44 | 58 |
|
45 | 59 | # 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') |
48 | 62 |
|
49 | 63 | # make colormaps available to call
|
50 | 64 | locals().update(cmap_d)
|
0 commit comments