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

How to get all colormap instannce ? #39

Open
CatNofishing opened this issue Jul 28, 2021 · 1 comment
Open

How to get all colormap instannce ? #39

CatNofishing opened this issue Jul 28, 2021 · 1 comment

Comments

@CatNofishing
Copy link

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

@manthey
Copy link

manthey commented Dec 17, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants