Skip to content

fix late binding and proper reversal for funcs #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions ultraplot/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,16 +1271,17 @@
--------
matplotlib.colors.LinearSegmentedColormap.reversed
"""

# Reverse segments
def _reverse_data(data):
if callable(data):
return lambda x, func=data: func(1 - x)
else:
return [(1.0 - x, y1, y0) for x, y0, y1 in reversed(data)]

segmentdata = {
key: (
(lambda x, func=data: func(x))
if callable(data)
else [(1.0 - x, y1, y0) for x, y0, y1 in reversed(data)]
)
for key, data in self._segmentdata.items()
key: _reverse_data(data) for key, data in self._segmentdata.items()
}

# Reverse gammas
if name is None:
name = self._make_name(suffix="r")
Expand Down Expand Up @@ -3137,7 +3138,7 @@
# Handle reversal
reverse = key.endswith("_r")
if reverse:
key = key.rstrip("_r")
key = key.removesuffix("_r")

# Check if the key exists in builtin colormaps
if self._has_item(key):
Expand All @@ -3156,7 +3157,7 @@

# Try mirroring the non-lowered key
if reverse:
original_key = original_key.strip("_r")
original_key = original_key.removesuffix("_r")

Check warning on line 3160 in ultraplot/colors.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/colors.py#L3160

Added line #L3160 was not covered by tests
half = len(original_key) // 2
mirrored_key = original_key[half:] + original_key[:half]
if self._has_item(mirrored_key):
Expand All @@ -3182,11 +3183,11 @@
key = self._translate_key(key, mirror=True)
shift = key.endswith("_s") and not self._has_item(key)
if shift:
key = key.rstrip("_s")
key = key.removesuffix("_s")

Check warning on line 3186 in ultraplot/colors.py

View check run for this annotation

Codecov / codecov/patch

ultraplot/colors.py#L3186

Added line #L3186 was not covered by tests
reverse = key.endswith("_r") and not self._has_item(key)

if reverse:
key = key.rstrip("_r")
key = key.removesuffix("_r")
# Retrieve colormap
if self._has_item(key):
value = self._cmaps[key].copy()
Expand Down
12 changes: 12 additions & 0 deletions ultraplot/tests/test_colormap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ultraplot as uplt, pytest, numpy as np


def test_colormap_reversal():
# Rainbow uses a callable which went wrong see
# https://github.com/Ultraplot/UltraPlot/issues/294#issuecomment-3016653770
cmap = uplt.Colormap("rainbow")
cmap_r = cmap.reversed()
for i in range(256):
assert np.allclose(
cmap(i), cmap_r(255 - i)
), f"Reversed colormap mismatch at index {i}"