jiffyclub / palettable

Color palettes for Python
https://jiffyclub.github.io/palettable/
Other
768 stars 73 forks source link

How to get all colormap instannce ? #39

Open CatNofishing opened 3 years ago

CatNofishing commented 3 years ago

How to get all colormap instannce ? I want to make one picture incluse all colormap

manthey commented 2 years ago

You can do something like this:

import types
from operator import attrgetter
import palettable

def recursePalettablePalettes(module, palettes, root=None, depth=0):
    """
    Walk the modules in palettable to find all of the available palettes.

    :param module: the current module.
    :param palettes: a set to add palette names to.
    :param root: a string of the parent modules.  None for palettable itself.
    :param depth: the depth of the walk.  Used to avoid needless recursion.
    """
    for key in dir(module):
        if not key.startswith('_'):
            attr = getattr(module, key)
            if isinstance(attr, types.ModuleType) and depth < 3:
                recursePalettablePalettes(
                    attr, palettes, root + '.' + key if root else key, depth + 1)
            elif root and isinstance(getattr(attr, 'hex_colors', None), list):
                palettes.add(root + '.' + key)

palettes = set()
_recursePalettablePalettes(palettable, palettes)
print(sorted(palettes))

And, of course, you can use a name like cartocolors.qualitative.Pastel_10 by just doing something like palette = attrgetter('cartocolors.qualitative.Pastel_10')(palettable).hex_colors.

It be nice there were an equivalent built in function.