-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ENH: set default color cycle to named color sequence #29808
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
Conversation
# color cycle for plot lines as list of string color specs: | ||
# single letter, long name, or web-style hex | ||
#axes.prop_cycle: cycler('color', 'tab10') | ||
# color cycle for plot lines as either a named color sequence or a list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As well as being more succinct, this saves me 40 microseconds 🚀
In [4]: %timeit validate_cycler("cycler('color', 'tab10')")
80.9 μs ± 1.25 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
In [5]: %timeit validate_cycler("cycler('color', ['1f77b4', 'ff7f0e', '2ca02c', 'd62728', '9467bd', '8c564b', 'e377c2', '7f7f7f', 'bcbd22', '17becf'])")
122 μs ± 952 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
lib/matplotlib/rcsetup.py
Outdated
colors = [scalar_validator(v.strip()) for v in s if v.strip()] | ||
except ValueError as ve: | ||
ke_msg = str(ke)[1:-1] # strip off quote marks | ||
raise ValueError(f'{ke_msg} and {ve}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd welcome any feedback on a better way to do the exception handling here. This works to combine the error messages from the two options (see new test) but I feel like I'm missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you simply
raise ValueError(f'{s!r} is neither a color sequence name nor can it be interpreted as a list of colors')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so my problem was trying to be too clever 😆
07a01d9
to
846e825
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not bother to discuss the potential ambiguity of color sequence name and single-letter-colors-in-string. AFAIK the potential single letter colors are "rgbcmykw" and it's hard to make reasonable name from that. So I doubt this will ever turn up in practice.
lib/matplotlib/rcsetup.py
Outdated
colors = [scalar_validator(v.strip()) for v in s if v.strip()] | ||
except ValueError as ve: | ||
ke_msg = str(ke)[1:-1] # strip off quote marks | ||
raise ValueError(f'{ke_msg} and {ve}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you simply
raise ValueError(f'{s!r} is neither a color sequence name nor can it be interpreted as a list of colors')
7fd60fc
to
2065369
Compare
#axes.prop_cycle: cycler('color', ['1f77b4', 'ff7f0e', '2ca02c', 'd62728', '9467bd', '8c564b', 'e377c2', '7f7f7f', 'bcbd22', '17becf']) | ||
# color cycle for plot lines as list of string color specs: | ||
# single letter, long name, or web-style hex | ||
#axes.prop_cycle: cycler(color='tab10') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍 such a nice improvement!
PR summary
Closes #29799
As mentioned in the issue, it is possible that someone could register a color sequence called something like "rygbk" and then it would be ambiguous whether to use the color sequence or interpret as single character colors. We agreed that the color sequence would take precedence in this situation. Should I actually document that precedence either in the user guide section or in a next-api-changes note? I think in practice it will come up very rarely if at all.
PR checklist