ENH: set default color cycle to named color sequence#29808
ENH: set default color cycle to named color sequence#29808dstansby merged 1 commit intomatplotlib:mainfrom
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.
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.
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.
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.
Ah, so my problem was trying to be too clever 😆
07a01d9 to
846e825
Compare
timhoffm
left a comment
There was a problem hiding this comment.
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.
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') |
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