diff --git a/ultraplot/internals/rcsetup.py b/ultraplot/internals/rcsetup.py index c854cfcc2..7439f35cf 100644 --- a/ultraplot/internals/rcsetup.py +++ b/ultraplot/internals/rcsetup.py @@ -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: @@ -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 @@ -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 diff --git a/ultraplot/tests/test_config.py b/ultraplot/tests/test_config.py index c700bb0c8..11a308b56 100644 --- a/ultraplot/tests/test_config.py +++ b/ultraplot/tests/test_config.py @@ -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