Skip to content

Commit 4ac496f

Browse files
authored
Feature: allow cycle objects to be set on rc (#378)
* add cycle parsing * allow lists * add unittest * update test with colormap and correct registration * black
1 parent cd4f876 commit 4ac496f

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

ultraplot/internals/rcsetup.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,14 @@ def _validate_cftime_resolution(unit: str) -> str:
295295
raise ValueError(msg)
296296

297297

298-
def _validate_cmap(subtype):
298+
def _validate_cmap(subtype, cycle=False):
299299
"""
300300
Validate the colormap or cycle. Possibly skip name registration check
301301
and assign the colormap name rather than a colormap instance.
302302
"""
303303

304304
def _validate_cmap(value):
305+
305306
name = value
306307
if isinstance(value, str):
307308
if VALIDATE_REGISTERED_CMAPS:
@@ -314,8 +315,15 @@ def _validate_cmap(value):
314315
if isinstance(name, str):
315316
from ..colors import _cmap_database # avoid circular imports
316317

317-
_cmap_database[name] = value
318+
_cmap_database.register(value, name=name)
318319
return name
320+
elif cycle:
321+
from ..constructor import Cycle
322+
323+
if isinstance(value, Cycler):
324+
return Cycle(value)
325+
elif np.iterable(value):
326+
return Cycle(value)
319327
raise ValueError(f"Invalid colormap or color cycle name {name!r}.")
320328

321329
return _validate_cmap
@@ -1195,7 +1203,7 @@ def copy(self):
11951203
# Color cycle additions
11961204
"cycle": (
11971205
CYCLE,
1198-
_validate_cmap("discrete"),
1206+
_validate_cmap("discrete", cycle=True),
11991207
"Name of the color cycle assigned to :rcraw:`axes.prop_cycle`.",
12001208
),
12011209
# Colormap additions

ultraplot/tests/test_config.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,29 @@ def test_dev_version_skipped(mock_urlopen, mock_version, mock_print):
9494

9595
check_for_update("fakepkg")
9696
mock_print.assert_not_called()
97+
98+
99+
@pytest.mark.parametrize(
100+
"cycle, raises_error",
101+
[
102+
("qual1", False),
103+
(["#5790fc", "#f89c20", "#e42536", "#964a8b", "#9c9ca1", "#7a21dd"], False),
104+
(
105+
uplt.constructor.Cycle(
106+
["#5790fc", "#f89c20", "#e42536", "#964a8b", "#9c9ca1", "#7a21dd"]
107+
),
108+
False,
109+
),
110+
(uplt.colormaps.get_cmap("viridis"), False),
111+
(1234, True),
112+
],
113+
)
114+
def test_cycle_rc_setting(cycle, raises_error):
115+
"""
116+
Test various ways to set the cycle in rc
117+
"""
118+
if raises_error:
119+
with pytest.raises(ValueError):
120+
uplt.rc["cycle"] = cycle
121+
else:
122+
uplt.rc["cycle"] = cycle

0 commit comments

Comments
 (0)