jiffyclub / palettable

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

feature request: high-level unique name for accessing palettes #23

Open andy-esch opened 6 years ago

andy-esch commented 6 years ago

Sometimes it can be hard knowing how to traverse palettable to get the palette you need. Unless I'm missing something, there does not seem to be a high level way of accessing palettes without prior knowledge about the structure of palettable. It'd be really handy to have a high-level get method to access a palette without advanced knowledge about how it's nested within palettable.

For instance, some palettes can be accessed like:

palettable.<class>.<module>.<palette_name>

E.g., palettable.colorbrewer.diverging.PRGn_11. This requires more information than just the palette name. Programmatically, it'd be accessed like the following if a user needed a specific palette:

>>> r = dict(cls='colorbrewer', module='diverging', palette='PRGn', n_colors='11')
>>> getattr(getattr(getattr(palettable, r['cls']), r['module']), r['palette'] + '_' + r['n_colors']).hex_colors
['#40004B', '#762A83', '#9970AB', '#C2A5CF', '#E7D4E8', '#F7F7F7', '#D9F0D3', '#A6DBA0', '#5AAE61', '#1B7837', '#00441B']

Others palettes are accessed like:

palettable.<class>.<palette_name>

For example, palettable.matplotlib.Inferno_13, which is much more easily accessible but still requires knowledge that the palette comes from matplotlib, which may or may not be relevant to the user.

It'd be really nice to have a high-level get method (or something like that) to retrieve palettes by a unique name that would return the palette object that you want to retrieve information from.

I can imagine something of the form:

>>> mpl_inf_13 = palettable.get('inferno', 13, reversed=True)
>>> mpl_inf_13.hex_colors
['#40004B', '#762A83', '#9970AB', '#C2A5CF', '#E7D4E8', '#F7F7F7', '#D9F0D3', '#A6DBA0', '#5AAE61', '#1B7837', '#00441B']

The user is only defining what they want, but it adds the additional constraint that no two palettes across the package can have the same name.

jiffyclub commented 6 years ago

Thanks for this suggestion! What are your thoughts on how folks would know that 'inferno' was a valid palette name in the first place? Docs?

m-beau commented 3 years ago

Hi, any update on this? There should really be a simple rules_them_all dict-like structure available in palettable, so that the unique colormap name is necessary to access the colormap.

I think that a dictionary structure is desirable, as dot indexing is not convenient when one doesn't want to use a specific colormap of the package but rather make all of them accessible within a function, through the use of a string fed as a parameter.