Skip to content
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

Add a registry for color sequences #22387

Merged
merged 1 commit into from
May 5, 2022
Merged

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Feb 3, 2022

Color sequences are simply lists of colors, that we store by name in
a registry. The registry is modelled similar to the ColormapRegistry
to 1) support immutable builtin color sequences and 2) to return copies
so that one cannot mess with the global definition of the color sequence
through an obtained instance.
Note that some details of ColormapRegistry are different and we need
different docstrings so that it's easier to have a separate class and not try
to factor out some common aspects in a base class.

For now, I've made the sequences used for ListedColormaps available
as builtin sequences, but that's open for discussion.

More usage documentation should be added in the color examples and/or
tutorials, but I'll wait with that till after the general approval of
the structure and API. One common use case will be

plt.rc_params['axes.prop_cycle'] = plt.cycler(color=plt.color_sequences['Pastel1')

try:
return list(self._color_sequences[item])
except KeyError:
raise KeyError(f"{item!r} is not a known color sequence name")
Copy link
Member

Choose a reason for hiding this comment

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

Should we include the list of known keys here?

@QuLogic
Copy link
Member

QuLogic commented Feb 4, 2022

So these are essentially colour palettes? Should they be named colour palettes? And if they are colour palettes, how does that jive with colour palettes in seaborn?

@jklymak
Copy link
Member

jklymak commented Feb 4, 2022

Yes maybe we should hoist the palette framework to matplotlib? Then in the context of this request we could add named_palette to take the name of a sequential colormap?

I think it may be confusing for us to have a parallel api to seaborn, so if we go a different way we should be very explicit about the differences.

@timhoffm
Copy link
Member Author

timhoffm commented Feb 4, 2022

seaborn.color_palette() is more a color sequence generator that a registry. That's much more than I had in mind, but we can discuss whether we want that functionality.

seaborn.color_palette(palette=None, n_colors=None, desat=None, as_cmap=False)

Apart from (1) returning some named palettes, it can create a palette:

  • by (2) sampling a matplotlib colormap
  • by (3) sampling from ‘husl’ or ‘hls’, cubehelix system (less configurable versions of the functions husl_palette, hls_palette, cubehelix_palette)
  • by (4) lightening or darkening a base color (see also light_palette, dark_palette)

(5) Additionally, one can choose the number of colors. This is quite nice.

(6) Additionally, one can desaturate the colors. This feels a bit much for the function. One could alternatively have a function desaturate(color_palette(...)) or make a slightly fancier list-like object, so that you can do color_palette(...).desaturate(...).

(7) Additionally, one can turn each of the palettes into a Colormap.

@timhoffm timhoffm added the status: needs comment/discussion needs consensus on next step label Feb 4, 2022
"""
Register a new color sequence.

The colormap registry stores a copy of the given *color_list*, so that
Copy link
Contributor

Choose a reason for hiding this comment

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

colormap -> color sequence

@timhoffm timhoffm marked this pull request as draft February 13, 2022 22:35
@timhoffm
Copy link
Member Author

timhoffm commented Feb 13, 2022

Moving to draft until we have decided whether and how far we want to go the seaborn way.

Next actions:

  • @timhoffm have a closer look at seaborn and come up with a proposal
  • Discussion in dev cal

@tacaswell
Copy link
Member

Talked about this on today's call: https://hackmd.io/jd_7FjxNQ4y7XgNknvmvGQ?view#color-registry

The consensus was

  1. keep the sequence registry simple as a mapping of str -> sequence of colors
  2. do not try to re-use the name palettes to avoid confusion with seaborn
  3. as a separate question add a (stateless) function for fabricating color sequences from simple input (in the spirit of seaborn.color_palette()).

@timhoffm timhoffm marked this pull request as ready for review February 24, 2022 23:09
@timhoffm
Copy link
Member Author

The existing PR should comply with everything we discussed in the dev call. Marking as ready-to-review.

@QuLogic
Copy link
Member

QuLogic commented Mar 1, 2022

The doc failures are relevant.

@timhoffm timhoffm removed the status: needs comment/discussion needs consensus on next step label Mar 3, 2022
Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

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

Do we want to have __contains__ to check for registered sequences?

doc/api/colors_api.rst Outdated Show resolved Hide resolved
lib/matplotlib/colors.py Outdated Show resolved Hide resolved
lib/matplotlib/tests/test_colors.py Outdated Show resolved Hide resolved
@timhoffm timhoffm force-pushed the color-sequences branch 2 times, most recently from 92dd83c to 3128f0d Compare May 5, 2022 20:24
@timhoffm
Copy link
Member Author

timhoffm commented May 5, 2022

__contains__ is mixed in through the use of collections.abc.Mapping
See https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes

@QuLogic
Copy link
Member

QuLogic commented May 5, 2022

Ah, I missed the class inheritance.

Color sequences are simply lists of colors, that we store by name in
a registry. The registry is modelled similar to the ColormapRegistry
to 1) support immutable builtin color sequences and 2) to return copies
so that one cannot mess with the global definition of the color sequence
through an obtained instance.

For now, I've made the sequences used for `ListedColormap`s available
as builtin sequences, but that's open for discussion.

More usage documentation should be added in the color examples and/or
tutorials, but I'll wait with that till after the general approval of
the structure and API. One common use case will be

```
plt.rc_params['axes.prop_cycle'] = plt.cycler(color=plt.color_sequences['Pastel1')
```

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
@QuLogic QuLogic merged commit 6997192 into matplotlib:main May 5, 2022
@timhoffm timhoffm deleted the color-sequences branch May 6, 2022 06:57
@QuLogic QuLogic mentioned this pull request Sep 9, 2022
2 tasks
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.

5 participants