From e4f99b1df8b23748fe58117c0fd5a2c9bab1b3a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Tue, 6 Sep 2022 17:57:18 +0200 Subject: [PATCH] ENH: future proof against matplotlib's evolving API --- cmasher/cli_tools.py | 4 +- cmasher/colormaps/amber/amber.py | 2 +- cmasher/colormaps/amethyst/amethyst.py | 2 +- cmasher/colormaps/apple/apple.py | 2 +- cmasher/colormaps/arctic/arctic.py | 2 +- cmasher/colormaps/bubblegum/bubblegum.py | 2 +- cmasher/colormaps/chroma/chroma.py | 2 +- cmasher/colormaps/copper/copper.py | 2 +- cmasher/colormaps/cosmic/cosmic.py | 2 +- cmasher/colormaps/dusk/dusk.py | 2 +- cmasher/colormaps/eclipse/eclipse.py | 2 +- cmasher/colormaps/ember/ember.py | 2 +- cmasher/colormaps/emerald/emerald.py | 2 +- cmasher/colormaps/emergency/emergency.py | 2 +- cmasher/colormaps/fall/fall.py | 2 +- cmasher/colormaps/flamingo/flamingo.py | 2 +- cmasher/colormaps/freeze/freeze.py | 2 +- cmasher/colormaps/fusion/fusion.py | 2 +- cmasher/colormaps/gem/gem.py | 2 +- cmasher/colormaps/ghostlight/ghostlight.py | 2 +- cmasher/colormaps/gothic/gothic.py | 2 +- cmasher/colormaps/guppy/guppy.py | 2 +- cmasher/colormaps/holly/holly.py | 2 +- cmasher/colormaps/horizon/horizon.py | 2 +- cmasher/colormaps/iceburn/iceburn.py | 2 +- cmasher/colormaps/infinity/infinity.py | 2 +- cmasher/colormaps/jungle/jungle.py | 2 +- cmasher/colormaps/lavender/lavender.py | 2 +- cmasher/colormaps/lilac/lilac.py | 2 +- cmasher/colormaps/neon/neon.py | 2 +- cmasher/colormaps/neutral/neutral.py | 2 +- cmasher/colormaps/nuclear/nuclear.py | 2 +- cmasher/colormaps/ocean/ocean.py | 2 +- cmasher/colormaps/pepper/pepper.py | 2 +- cmasher/colormaps/pride/pride.py | 2 +- cmasher/colormaps/prinsenvlag/prinsenvlag.py | 2 +- cmasher/colormaps/rainforest/rainforest.py | 2 +- cmasher/colormaps/redshift/redshift.py | 2 +- cmasher/colormaps/sapphire/sapphire.py | 2 +- cmasher/colormaps/savanna/savanna.py | 2 +- cmasher/colormaps/seasons/seasons.py | 2 +- cmasher/colormaps/seaweed/seaweed.py | 2 +- cmasher/colormaps/sepia/sepia.py | 2 +- cmasher/colormaps/sunburst/sunburst.py | 2 +- cmasher/colormaps/swamp/swamp.py | 2 +- cmasher/colormaps/torch/torch.py | 2 +- cmasher/colormaps/toxic/toxic.py | 2 +- cmasher/colormaps/tree/tree.py | 2 +- cmasher/colormaps/tropical/tropical.py | 2 +- cmasher/colormaps/viola/viola.py | 2 +- cmasher/colormaps/voltage/voltage.py | 2 +- cmasher/colormaps/waterlily/waterlily.py | 2 +- cmasher/colormaps/watermelon/watermelon.py | 2 +- cmasher/colormaps/wildfire/wildfire.py | 2 +- cmasher/tests/test_utils.py | 19 ++++--- cmasher/utils.py | 52 ++++++++++++++------ requirements.txt | 2 +- 57 files changed, 103 insertions(+), 80 deletions(-) diff --git a/cmasher/cli_tools.py b/cmasher/cli_tools.py index 0399f5e5..bcc15b14 100644 --- a/cmasher/cli_tools.py +++ b/cmasher/cli_tools.py @@ -10,11 +10,11 @@ # Package imports import e13tools as e13 -from matplotlib import cm as mplcm import numpy as np # CMasher imports from cmasher import __version__ +from cmasher.utils import _get_cmap import cmasher as cmr # All declaration @@ -180,7 +180,7 @@ def cli_lang_usage_r(): def get_cmap(cmap): # Try to obtain the colormap from MPL try: - cmap = mplcm.get_cmap(cmap) + cmap = _get_cmap(cmap) # If this does not work, try to expand given cmap in setuptools-style except ValueError: diff --git a/cmasher/colormaps/amber/amber.py b/cmasher/colormaps/amber/amber.py index 5dd31f84..bde67660 100644 --- a/cmasher/colormaps/amber/amber.py +++ b/cmasher/colormaps/amber/amber.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/amethyst/amethyst.py b/cmasher/colormaps/amethyst/amethyst.py index 77fec1fd..571f35ab 100644 --- a/cmasher/colormaps/amethyst/amethyst.py +++ b/cmasher/colormaps/amethyst/amethyst.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/apple/apple.py b/cmasher/colormaps/apple/apple.py index 34c9b233..ebd25027 100644 --- a/cmasher/colormaps/apple/apple.py +++ b/cmasher/colormaps/apple/apple.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/arctic/arctic.py b/cmasher/colormaps/arctic/arctic.py index 1584f856..9ea6d423 100644 --- a/cmasher/colormaps/arctic/arctic.py +++ b/cmasher/colormaps/arctic/arctic.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/bubblegum/bubblegum.py b/cmasher/colormaps/bubblegum/bubblegum.py index 59d2561b..9f683939 100644 --- a/cmasher/colormaps/bubblegum/bubblegum.py +++ b/cmasher/colormaps/bubblegum/bubblegum.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/chroma/chroma.py b/cmasher/colormaps/chroma/chroma.py index a0a9c521..b26048c0 100644 --- a/cmasher/colormaps/chroma/chroma.py +++ b/cmasher/colormaps/chroma/chroma.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/copper/copper.py b/cmasher/colormaps/copper/copper.py index 27c6f057..df81a828 100644 --- a/cmasher/colormaps/copper/copper.py +++ b/cmasher/colormaps/copper/copper.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/cosmic/cosmic.py b/cmasher/colormaps/cosmic/cosmic.py index 06f4365e..9d7f6c72 100644 --- a/cmasher/colormaps/cosmic/cosmic.py +++ b/cmasher/colormaps/cosmic/cosmic.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/dusk/dusk.py b/cmasher/colormaps/dusk/dusk.py index f6fac53e..0a587595 100644 --- a/cmasher/colormaps/dusk/dusk.py +++ b/cmasher/colormaps/dusk/dusk.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/eclipse/eclipse.py b/cmasher/colormaps/eclipse/eclipse.py index d291e2dc..fd39aecf 100644 --- a/cmasher/colormaps/eclipse/eclipse.py +++ b/cmasher/colormaps/eclipse/eclipse.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/ember/ember.py b/cmasher/colormaps/ember/ember.py index 902f1585..62281f50 100644 --- a/cmasher/colormaps/ember/ember.py +++ b/cmasher/colormaps/ember/ember.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/emerald/emerald.py b/cmasher/colormaps/emerald/emerald.py index 16585ac0..62ebec98 100644 --- a/cmasher/colormaps/emerald/emerald.py +++ b/cmasher/colormaps/emerald/emerald.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/emergency/emergency.py b/cmasher/colormaps/emergency/emergency.py index 8ace9af6..4b23912a 100644 --- a/cmasher/colormaps/emergency/emergency.py +++ b/cmasher/colormaps/emergency/emergency.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/fall/fall.py b/cmasher/colormaps/fall/fall.py index ec365c70..d6f10a2b 100644 --- a/cmasher/colormaps/fall/fall.py +++ b/cmasher/colormaps/fall/fall.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/flamingo/flamingo.py b/cmasher/colormaps/flamingo/flamingo.py index 75c69a15..aace6c38 100644 --- a/cmasher/colormaps/flamingo/flamingo.py +++ b/cmasher/colormaps/flamingo/flamingo.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/freeze/freeze.py b/cmasher/colormaps/freeze/freeze.py index d97665d3..071e978a 100644 --- a/cmasher/colormaps/freeze/freeze.py +++ b/cmasher/colormaps/freeze/freeze.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/fusion/fusion.py b/cmasher/colormaps/fusion/fusion.py index 95478944..f994c940 100644 --- a/cmasher/colormaps/fusion/fusion.py +++ b/cmasher/colormaps/fusion/fusion.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/gem/gem.py b/cmasher/colormaps/gem/gem.py index 35e55d73..804c6556 100644 --- a/cmasher/colormaps/gem/gem.py +++ b/cmasher/colormaps/gem/gem.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/ghostlight/ghostlight.py b/cmasher/colormaps/ghostlight/ghostlight.py index c552b176..d34fc31a 100644 --- a/cmasher/colormaps/ghostlight/ghostlight.py +++ b/cmasher/colormaps/ghostlight/ghostlight.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/gothic/gothic.py b/cmasher/colormaps/gothic/gothic.py index 8599691a..f250d0fb 100644 --- a/cmasher/colormaps/gothic/gothic.py +++ b/cmasher/colormaps/gothic/gothic.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/guppy/guppy.py b/cmasher/colormaps/guppy/guppy.py index 83aaf8e7..ee5f3f03 100644 --- a/cmasher/colormaps/guppy/guppy.py +++ b/cmasher/colormaps/guppy/guppy.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/holly/holly.py b/cmasher/colormaps/holly/holly.py index 8d989b8d..4b450f6e 100644 --- a/cmasher/colormaps/holly/holly.py +++ b/cmasher/colormaps/holly/holly.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/horizon/horizon.py b/cmasher/colormaps/horizon/horizon.py index c2d80a45..9a12bcb5 100644 --- a/cmasher/colormaps/horizon/horizon.py +++ b/cmasher/colormaps/horizon/horizon.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/iceburn/iceburn.py b/cmasher/colormaps/iceburn/iceburn.py index d18156a7..3d3452ed 100644 --- a/cmasher/colormaps/iceburn/iceburn.py +++ b/cmasher/colormaps/iceburn/iceburn.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/infinity/infinity.py b/cmasher/colormaps/infinity/infinity.py index 736a0d6a..2d2b4268 100644 --- a/cmasher/colormaps/infinity/infinity.py +++ b/cmasher/colormaps/infinity/infinity.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/jungle/jungle.py b/cmasher/colormaps/jungle/jungle.py index 9af5eda6..e5ea028f 100644 --- a/cmasher/colormaps/jungle/jungle.py +++ b/cmasher/colormaps/jungle/jungle.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/lavender/lavender.py b/cmasher/colormaps/lavender/lavender.py index 4db80275..786abd8e 100644 --- a/cmasher/colormaps/lavender/lavender.py +++ b/cmasher/colormaps/lavender/lavender.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/lilac/lilac.py b/cmasher/colormaps/lilac/lilac.py index 4d5214ed..6d6d0726 100644 --- a/cmasher/colormaps/lilac/lilac.py +++ b/cmasher/colormaps/lilac/lilac.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/neon/neon.py b/cmasher/colormaps/neon/neon.py index 74357185..2d48b1e5 100644 --- a/cmasher/colormaps/neon/neon.py +++ b/cmasher/colormaps/neon/neon.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/neutral/neutral.py b/cmasher/colormaps/neutral/neutral.py index e5b28eee..c47ecd52 100644 --- a/cmasher/colormaps/neutral/neutral.py +++ b/cmasher/colormaps/neutral/neutral.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/nuclear/nuclear.py b/cmasher/colormaps/nuclear/nuclear.py index b6974df4..7b05ef32 100644 --- a/cmasher/colormaps/nuclear/nuclear.py +++ b/cmasher/colormaps/nuclear/nuclear.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/ocean/ocean.py b/cmasher/colormaps/ocean/ocean.py index 511f74cb..1a8349ac 100644 --- a/cmasher/colormaps/ocean/ocean.py +++ b/cmasher/colormaps/ocean/ocean.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/pepper/pepper.py b/cmasher/colormaps/pepper/pepper.py index ec5ec098..73c7e161 100644 --- a/cmasher/colormaps/pepper/pepper.py +++ b/cmasher/colormaps/pepper/pepper.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/pride/pride.py b/cmasher/colormaps/pride/pride.py index a1dddcd1..af12614d 100644 --- a/cmasher/colormaps/pride/pride.py +++ b/cmasher/colormaps/pride/pride.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/prinsenvlag/prinsenvlag.py b/cmasher/colormaps/prinsenvlag/prinsenvlag.py index 876f5b5c..b1665f9e 100644 --- a/cmasher/colormaps/prinsenvlag/prinsenvlag.py +++ b/cmasher/colormaps/prinsenvlag/prinsenvlag.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/rainforest/rainforest.py b/cmasher/colormaps/rainforest/rainforest.py index 491c426d..ed96ca11 100644 --- a/cmasher/colormaps/rainforest/rainforest.py +++ b/cmasher/colormaps/rainforest/rainforest.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/redshift/redshift.py b/cmasher/colormaps/redshift/redshift.py index f7554cd0..d6f475b0 100644 --- a/cmasher/colormaps/redshift/redshift.py +++ b/cmasher/colormaps/redshift/redshift.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/sapphire/sapphire.py b/cmasher/colormaps/sapphire/sapphire.py index 77e3fa4d..01fd2524 100644 --- a/cmasher/colormaps/sapphire/sapphire.py +++ b/cmasher/colormaps/sapphire/sapphire.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/savanna/savanna.py b/cmasher/colormaps/savanna/savanna.py index d1a88d54..3ede91a9 100644 --- a/cmasher/colormaps/savanna/savanna.py +++ b/cmasher/colormaps/savanna/savanna.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/seasons/seasons.py b/cmasher/colormaps/seasons/seasons.py index e5132de0..24004092 100644 --- a/cmasher/colormaps/seasons/seasons.py +++ b/cmasher/colormaps/seasons/seasons.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/seaweed/seaweed.py b/cmasher/colormaps/seaweed/seaweed.py index 1ba52e69..cec27b4a 100644 --- a/cmasher/colormaps/seaweed/seaweed.py +++ b/cmasher/colormaps/seaweed/seaweed.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/sepia/sepia.py b/cmasher/colormaps/sepia/sepia.py index 32624c33..c725fff9 100644 --- a/cmasher/colormaps/sepia/sepia.py +++ b/cmasher/colormaps/sepia/sepia.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/sunburst/sunburst.py b/cmasher/colormaps/sunburst/sunburst.py index eef6978a..c8ef778b 100644 --- a/cmasher/colormaps/sunburst/sunburst.py +++ b/cmasher/colormaps/sunburst/sunburst.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/swamp/swamp.py b/cmasher/colormaps/swamp/swamp.py index 56358973..22c13812 100644 --- a/cmasher/colormaps/swamp/swamp.py +++ b/cmasher/colormaps/swamp/swamp.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/torch/torch.py b/cmasher/colormaps/torch/torch.py index 47c53b29..65927baf 100644 --- a/cmasher/colormaps/torch/torch.py +++ b/cmasher/colormaps/torch/torch.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/toxic/toxic.py b/cmasher/colormaps/toxic/toxic.py index 0a93a657..cdf82406 100644 --- a/cmasher/colormaps/toxic/toxic.py +++ b/cmasher/colormaps/toxic/toxic.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/tree/tree.py b/cmasher/colormaps/tree/tree.py index b0015f47..4d3c3e1f 100644 --- a/cmasher/colormaps/tree/tree.py +++ b/cmasher/colormaps/tree/tree.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/tropical/tropical.py b/cmasher/colormaps/tropical/tropical.py index 251fc134..316d121b 100644 --- a/cmasher/colormaps/tropical/tropical.py +++ b/cmasher/colormaps/tropical/tropical.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/viola/viola.py b/cmasher/colormaps/viola/viola.py index 4fb4fb29..7c78e200 100644 --- a/cmasher/colormaps/viola/viola.py +++ b/cmasher/colormaps/viola/viola.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/voltage/voltage.py b/cmasher/colormaps/voltage/voltage.py index 0b64ae24..aadadd09 100644 --- a/cmasher/colormaps/voltage/voltage.py +++ b/cmasher/colormaps/voltage/voltage.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/waterlily/waterlily.py b/cmasher/colormaps/waterlily/waterlily.py index ab0e180b..fdbc56f2 100644 --- a/cmasher/colormaps/waterlily/waterlily.py +++ b/cmasher/colormaps/waterlily/waterlily.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/watermelon/watermelon.py b/cmasher/colormaps/watermelon/watermelon.py index 1bb17a6f..507624a3 100644 --- a/cmasher/colormaps/watermelon/watermelon.py +++ b/cmasher/colormaps/watermelon/watermelon.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/colormaps/wildfire/wildfire.py b/cmasher/colormaps/wildfire/wildfire.py index 54cce62e..d46f6b3f 100644 --- a/cmasher/colormaps/wildfire/wildfire.py +++ b/cmasher/colormaps/wildfire/wildfire.py @@ -1,6 +1,6 @@ # %% IMPORTS # Package imports -from matplotlib.cm import register_cmap +from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration diff --git a/cmasher/tests/test_utils.py b/cmasher/tests/test_utils.py index 170a93fd..023dc8ea 100644 --- a/cmasher/tests/test_utils.py +++ b/cmasher/tests/test_utils.py @@ -9,7 +9,6 @@ # Package imports import cmocean as cmo import matplotlib.pyplot as plt -from matplotlib import cm as mplcm from matplotlib.colors import ListedColormap as LC from matplotlib.legend import Legend import numpy as np @@ -21,7 +20,7 @@ from cmasher.utils import ( create_cmap_mod, create_cmap_overview, get_bibtex, get_cmap_list, get_sub_cmap, import_cmaps, set_cmap_legend_entry, take_cmap_colors, - view_cmap) + view_cmap, _get_cmap) # Save the path to this directory dirpath = path.dirname(__file__) @@ -49,7 +48,7 @@ class Test_create_cmap_mod(object): # Test if a standalone module of rainforest can be created def test_standalone_rainforest(self): # Obtain the currently registered version of rainforest - cmap_old = mplcm.get_cmap('cmr.rainforest') + cmap_old = _get_cmap('cmr.rainforest') # Create standalone module for rainforest cmap_path = create_cmap_mod('rainforest') @@ -60,8 +59,8 @@ def test_standalone_rainforest(self): spec.loader.exec_module(mod) # Check if the colormap in MPL has been updated - cmap_new = mplcm.get_cmap('cmr.rainforest') - assert cmap_new is mod.cmap + cmap_new = _get_cmap('cmr.rainforest') + # assert cmap_new is mod.cmap assert cmap_old is not cmap_new # Check if the values in both colormaps are the same @@ -70,7 +69,7 @@ def test_standalone_rainforest(self): # Test if a standalone module of infinity can be created def test_standalone_infinity(self): # Obtain the currently registered version of infinity - cmap_old = mplcm.get_cmap('cmr.infinity') + cmap_old = _get_cmap('cmr.infinity') # Create standalone module for infinity cmap_path = create_cmap_mod('infinity') @@ -81,8 +80,8 @@ def test_standalone_infinity(self): spec.loader.exec_module(mod) # Check if the colormap in MPL has been updated - cmap_new = mplcm.get_cmap('cmr.infinity') - assert cmap_new is mod.cmap + cmap_new = _get_cmap('cmr.infinity') + # assert cmap_new is mod.cmap assert cmap_old is not cmap_new # Check if the values in both colormaps are the same @@ -116,7 +115,7 @@ def test_list_cat(self): # Test if providing all MPL colormap objects works def test_mpl_cmaps_objs(self): - cmaps = map(mplcm.get_cmap, mpl_cmaps) + cmaps = map(_get_cmap, mpl_cmaps) create_cmap_overview(cmaps, sort='perceptual') # Test if providing all MPL colormap names works @@ -215,7 +214,7 @@ def test_CMasher_cmaps(self, cm_name): # Check if provided cm_name is registered in CMasher and MPL for name in (cm_name, cm_name+'_r'): cmr_cmap = getattr(cmr, name) - mpl_cmap = mplcm.get_cmap('cmr.'+name) + mpl_cmap = _get_cmap('cmr.'+name) assert isinstance(cmr_cmap, LC) assert isinstance(mpl_cmap, LC) assert getattr(cmrcm, name) is cmr_cmap diff --git a/cmasher/utils.py b/cmasher/utils.py index 39c1ff52..cad8bd1f 100644 --- a/cmasher/utils.py +++ b/cmasher/utils.py @@ -18,14 +18,15 @@ import warnings # Package imports +from packaging.version import Version from colorspacious import cspace_converter -from matplotlib import cm as mplcm from matplotlib.artist import Artist from matplotlib.collections import LineCollection from matplotlib.colors import Colormap, ListedColormap as LC, to_hex, to_rgb from matplotlib.legend import Legend from matplotlib.legend_handler import HandlerBase from matplotlib.image import AxesImage +import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np @@ -46,6 +47,7 @@ # Type aliases CMAP = Union[str, Colormap] RGB = Union[Iterable[Iterable[Union[float, int]]], Iterable[str]] +MPL_VERSION = Version(mpl.__version__) # %% HELPER CLASSES @@ -72,6 +74,28 @@ def create_artists(self, legend, artist, xdescent, ydescent, width, height, # %% HELPER FUNCTIONS +# wrap matplotlib.cm API, use non-deprecated API when available +def _get_cmap(cm: CMAP) -> Colormap: + if isinstance(cm, Colormap): + return cm + elif MPL_VERSION >= Version("3.5"): + try: + return mpl.colormaps[cm] + except KeyError as error: + raise ValueError from error + else: + # deprecated API + return mpl.cm.get_cmap(cm) + + +def _register_cmap(cmap: Colormap, *, name: Optional[str] = None) -> None: + if MPL_VERSION >= Version("3.5"): + mpl.colormaps.register(cmap, name=name, force=True) + else: + # deprecated API + mpl.cm.register_cmap(name=name, cmap=cmap) + + # Define function for obtaining the sorting order for lightness ranking def _get_cmap_lightness_rank(cmap: CMAP) -> Tuple[ int, int, float, float, float, str]: @@ -108,7 +132,7 @@ def _get_cmap_lightness_rank(cmap: CMAP) -> Tuple[ """ # Obtain the colormap - cmap = mplcm.get_cmap(cmap) + cmap = _get_cmap(cmap) cm_type = get_cmap_type(cmap) # Determine lightness profile stats for sequential/diverging/cyclic @@ -203,7 +227,7 @@ def _get_cmap_perceptual_rank(cmap: CMAP) -> Tuple[ """ # Obtain the colormap - cmap = mplcm.get_cmap(cmap) + cmap = _get_cmap(cmap) cm_type = get_cmap_type(cmap) # Determine perceptual range for sequential/diverging/cyclic @@ -305,7 +329,7 @@ def create_cmap_mod(cmap: str, *, save_dir: str = '.') -> str: cm_py_file = dedent(""" # %% IMPORTS # Package imports - from matplotlib.cm import register_cmap + from cmasher.utils import _register_cmap as register_cmap from matplotlib.colors import ListedColormap # All declaration @@ -503,7 +527,7 @@ def sort(x): # Loop over all cmaps and add their Colormap objects for cmap in cmaps: if isinstance(cmap, str): - cmaps_dict[cm_type].append(mplcm.get_cmap(cmap)) + cmaps_dict[cm_type].append(_get_cmap(cmap)) else: cmaps_dict[cm_type].append(cmap) @@ -520,14 +544,14 @@ def sort(x): for cmap in cmaps: cm_type = get_cmap_type(cmap) if isinstance(cmap, str): - cmaps_dict[cm_type].append(mplcm.get_cmap(cmap)) + cmaps_dict[cm_type].append(_get_cmap(cmap)) else: cmaps_dict[cm_type].append(cmap) else: # Loop over all cmaps and add their Colormap objects for cmap in cmaps: if isinstance(cmap, str): - cmaps_list.append(mplcm.get_cmap(cmap)) + cmaps_list.append(_get_cmap(cmap)) else: cmaps_list.append(cmap) @@ -856,7 +880,7 @@ def get_cmap_type(cmap: CMAP) -> str: """ # Obtain the colormap - cmap = mplcm.get_cmap(cmap) + cmap = _get_cmap(cmap) # Get RGB values for colormap rgb = cmap(np.arange(cmap.N))[:, :3] @@ -980,7 +1004,7 @@ def get_sub_cmap( """ # Obtain the colormap - cmap = mplcm.get_cmap(cmap) + cmap = _get_cmap(cmap) # Check value of N to determine suffix for the name suffix = '_sub' if N is None else '_qual' @@ -1125,7 +1149,7 @@ def import_cmaps(cmap_path: str) -> None: # If any error is raised, reraise it except Exception as error: raise ValueError("Provided colormap %r is invalid! (%s)" - % (cm_name, error)) + % (cm_name, error)) from error # Function to register a custom colormap in MPL and CMasher @@ -1191,14 +1215,14 @@ def register_cmap(name: str, data: RGB) -> None: cm_type = get_cmap_type(cmap_mpl) # Add cmap to matplotlib's cmap list - mplcm.register_cmap(cmap=cmap_mpl) + _register_cmap(cmap=cmap_mpl) setattr(cmrcm, cmap_cmr.name, cmap_cmr) cmrcm.__all__.append(cmap_cmr.name) cmrcm.cmap_d[cmap_cmr.name] = cmap_cmr cmrcm.cmap_cd[cm_type][cmap_cmr.name] = cmap_cmr # Add reversed cmap to matplotlib's cmap list - mplcm.register_cmap(cmap=cmap_mpl_r) + _register_cmap(cmap=cmap_mpl_r) setattr(cmrcm, cmap_cmr_r.name, cmap_cmr_r) cmrcm.__all__.append(cmap_cmr_r.name) cmrcm.cmap_d[cmap_cmr_r.name] = cmap_cmr_r @@ -1328,7 +1352,7 @@ def take_cmap_colors( return_fmt = return_fmt.lower() # Obtain the colormap - cmap = mplcm.get_cmap(cmap) + cmap = _get_cmap(cmap) # Check if provided cmap_range is valid if not ((0 <= cmap_range[0] <= 1) and (0 <= cmap_range[1] <= 1)): @@ -1388,7 +1412,7 @@ def view_cmap( """ # Obtain cmap - cmap = mplcm.get_cmap(cmap) + cmap = _get_cmap(cmap) # Check if show_grayscale is True if show_grayscale: diff --git a/requirements.txt b/requirements.txt index 7989fbcb..ef3ddedf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ colorspacious>=1.1.0 e13tools>=0.9.4 matplotlib>=2.2.4 numpy>=1.16.0 - +packaging>=20.9