Skip to content
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
14 changes: 11 additions & 3 deletions ultraplot/internals/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,14 @@ def _validate_cftime_resolution(unit: str) -> str:
raise ValueError(msg)


def _validate_cmap(subtype):
def _validate_cmap(subtype, cycle=False):
"""
Validate the colormap or cycle. Possibly skip name registration check
and assign the colormap name rather than a colormap instance.
"""

def _validate_cmap(value):

name = value
if isinstance(value, str):
if VALIDATE_REGISTERED_CMAPS:
Expand All @@ -314,8 +315,15 @@ def _validate_cmap(value):
if isinstance(name, str):
from ..colors import _cmap_database # avoid circular imports

_cmap_database[name] = value
_cmap_database.register(value, name=name)
return name
elif cycle:
from ..constructor import Cycle

if isinstance(value, Cycler):
return Cycle(value)
elif np.iterable(value):
return Cycle(value)
raise ValueError(f"Invalid colormap or color cycle name {name!r}.")

return _validate_cmap
Expand Down Expand Up @@ -1195,7 +1203,7 @@ def copy(self):
# Color cycle additions
"cycle": (
CYCLE,
_validate_cmap("discrete"),
_validate_cmap("discrete", cycle=True),
"Name of the color cycle assigned to :rcraw:`axes.prop_cycle`.",
),
# Colormap additions
Expand Down
26 changes: 26 additions & 0 deletions ultraplot/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,29 @@ def test_dev_version_skipped(mock_urlopen, mock_version, mock_print):

check_for_update("fakepkg")
mock_print.assert_not_called()


@pytest.mark.parametrize(
"cycle, raises_error",
[
("qual1", False),
(["#5790fc", "#f89c20", "#e42536", "#964a8b", "#9c9ca1", "#7a21dd"], False),
(
uplt.constructor.Cycle(
["#5790fc", "#f89c20", "#e42536", "#964a8b", "#9c9ca1", "#7a21dd"]
),
False,
),
(uplt.colormaps.get_cmap("viridis"), False),
(1234, True),
],
)
def test_cycle_rc_setting(cycle, raises_error):
"""
Test various ways to set the cycle in rc
"""
if raises_error:
with pytest.raises(ValueError):
uplt.rc["cycle"] = cycle
else:
uplt.rc["cycle"] = cycle