Skip to content

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

Merged
merged 1 commit into from
Mar 28, 2025

Conversation

rcomer
Copy link
Member

@rcomer rcomer commented Mar 26, 2025

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

# 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
Copy link
Member Author

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)

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}')
Copy link
Member Author

@rcomer rcomer Mar 26, 2025

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.

Copy link
Member

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')

Copy link
Member Author

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 😆

Copy link
Member

@timhoffm timhoffm left a 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.

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}')
Copy link
Member

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')

@rcomer rcomer marked this pull request as draft March 27, 2025 12:07
@rcomer rcomer force-pushed the color-sequence-rcparam branch from 7fd60fc to 2065369 Compare March 27, 2025 14:55
@rcomer rcomer marked this pull request as ready for review March 27, 2025 16:10
#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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍 such a nice improvement!

@dstansby dstansby added this to the v3.11.0 milestone Mar 28, 2025
@dstansby dstansby merged commit a1aa994 into matplotlib:main Mar 28, 2025
40 of 41 checks passed
@rcomer rcomer deleted the color-sequence-rcparam branch March 28, 2025 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ENH]: set default color cycle to named color sequence
4 participants