Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ENH]: Suggest 'CN' if color is an integer #27378

Closed
@boeddeker

Description

@boeddeker

Problem

It took me some year to figure out, that matplotlib supports 'CN' (e.g. 'C1') as color.
I tried 1 (integer), but that raises only a ValueError: 1 is not a valid value for color (triggered by plt.plot(np.arange(10), color=1))
and when I implemented a workaround to support integers in our wrappers, a colleague asked about the difference to 'CN'.

Proposed solution

Current code:

def _check_color_like(**kwargs):
"""
For each *key, value* pair in *kwargs*, check that *value* is color-like.
"""
for k, v in kwargs.items():
if not is_color_like(v):
raise ValueError(f"{v!r} is not a valid value for {k}")

Suggestion:
Add some more text to the exception message, when the type is an integer, e.g.:

def _check_color_like(**kwargs): 
     """ 
     For each *key, value* pair in *kwargs*, check that *value* is color-like. 
     """ 
     for k, v in kwargs.items(): 
         if not is_color_like(v): 
             if isinstance(v, int) and k == "color" and v >= 0:
                 raise ValueError(
                     f"{v!r} is not a valid value for {k}.\n"
                     f"To get the N'th entry from mpl.rcParams['axes.prop_cycle'] you can use 'CN' instead of an integer.") 
             raise ValueError(f"{v!r} is not a valid value for {k}") 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions