Skip to content

Commit

Permalink
BUG: fix compatibility with matplotlib 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Dec 17, 2023
1 parent 2cd137a commit 8e6d3de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmasher/cli_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def cli_lang_usage_r():
def get_cmap(cmap):
# Try to obtain the colormap from MPL
try:
cmap = mpl.colormaps.get_cmap(cmap)
cmap = mpl.colormaps[cmap]

# If this does not work, try to expand given cmap in setuptools-style
except ValueError:
Expand Down
5 changes: 3 additions & 2 deletions cmasher/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def test_standalone_rainforest(self):
# Check if the colormap in MPL has been updated
cmap_new = mpl.colormaps["cmr.rainforest_copy"]

# identity equality isn't achievable since mpl.colormaps.get_cmap may return a copy
# identity equality isn't achievable since mpl.colormaps.__getitem__
# may return a copy
assert cmap_new == mod.cmap
assert cmap_old == cmap_new

Expand Down Expand Up @@ -124,7 +125,7 @@ def test_list_cat(self):

# Test if providing all MPL colormap objects works
def test_mpl_cmaps_objs(self):
cmaps = map(mpl.colormaps.get_cmap, mpl_cmaps)
cmaps = map(mpl.colormaps.__getitem__, mpl_cmaps)
create_cmap_overview(cmaps, sort="perceptual")

# Test if providing all MPL colormap names works
Expand Down
27 changes: 18 additions & 9 deletions cmasher/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def _get_cmap_lightness_rank(cmap: CMAP) -> tuple[int, int, float, float, float,
"""
# Obtain the colormap
cmap = mpl.colormaps.get_cmap(cmap)
if isinstance(cmap, str):
cmap = mpl.colormaps[cmap]

Check warning on line 122 in cmasher/utils.py

View check run for this annotation

Codecov / codecov/patch

cmasher/utils.py#L122

Added line #L122 was not covered by tests

cm_type = get_cmap_type(cmap)

# Determine lightness profile stats for sequential/diverging/cyclic
Expand Down Expand Up @@ -216,7 +218,9 @@ def _get_cmap_perceptual_rank(
"""
# Obtain the colormap
cmap = mpl.colormaps.get_cmap(cmap)
if isinstance(cmap, str):
cmap = mpl.colormaps[cmap]

Check warning on line 222 in cmasher/utils.py

View check run for this annotation

Codecov / codecov/patch

cmasher/utils.py#L222

Added line #L222 was not covered by tests

cm_type = get_cmap_type(cmap)

# Determine perceptual range for sequential/diverging/cyclic
Expand Down Expand Up @@ -300,6 +304,9 @@ def create_cmap_mod(

# Obtain the CMasher colormap associated with the provided cmap
cmap = cmrcm.cmap_d.get(name, None)
if cmap is None:
raise ValueError(f"{name!r} is not a valid cmasher colormap name")

cm_type = get_cmap_type(cmap)

# If cmap is None, raise error
Expand Down Expand Up @@ -530,7 +537,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(mpl.colormaps.get_cmap(cmap))
cmaps_dict[cm_type].append(mpl.colormaps[cmap])
else:
cmaps_dict[cm_type].append(cmap)

Expand All @@ -546,14 +553,14 @@ def sort(x):
for cmap in cmaps:
cm_type = get_cmap_type(cmap)
if isinstance(cmap, str):
cmaps_dict[cm_type].append(mpl.colormaps.get_cmap(cmap))
cmaps_dict[cm_type].append(mpl.colormaps[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(mpl.colormaps.get_cmap(cmap))
cmaps_list.append(mpl.colormaps[cmap])
else:
cmaps_list.append(cmap)

Expand Down Expand Up @@ -922,7 +929,8 @@ def get_cmap_type(cmap: CMAP) -> str:
"""
# Obtain the colormap
cmap = mpl.colormaps.get_cmap(cmap)
if isinstance(cmap, str):
cmap = mpl.colormaps[cmap]

# Get RGB values for colormap
rgb = cmap(np.arange(cmap.N))[:, :3]
Expand Down Expand Up @@ -1042,7 +1050,7 @@ def get_sub_cmap(
"""
# Obtain the colormap
cmap = mpl.colormaps.get_cmap(cmap)
cmap = mpl.colormaps[cmap]

# Check value of N to determine suffix for the name
suffix = "_sub" if N is None else "_qual"
Expand Down Expand Up @@ -1403,7 +1411,8 @@ def take_cmap_colors(
return_fmt = return_fmt.lower()

# Obtain the colormap
cmap = mpl.colormaps.get_cmap(cmap)
if isinstance(cmap, str):
cmap = mpl.colormaps[cmap]

# Check if provided cmap_range is valid
if not ((0 <= cmap_range[0] <= 1) and (0 <= cmap_range[1] <= 1)):
Expand Down Expand Up @@ -1464,7 +1473,7 @@ def view_cmap(
"""
# Obtain cmap
cmap = mpl.colormaps.get_cmap(cmap)
cmap = mpl.colormaps[cmap]

# Check if show_grayscale is True
if show_grayscale:
Expand Down

0 comments on commit 8e6d3de

Please sign in to comment.