jiffyclub / palettable

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

Add simple `make_palette` interface #17

Open olgabot opened 7 years ago

olgabot commented 7 years ago

Right now, there's a barrier for me to use palettable directly because I have to remember to include the number of colors I want for each colormap when I do the import, and I don't always know where I want to look for the palette I want. Could there be a palettable.make_palette("palettename", n_colors=8) - type interface?

jiffyclub commented 7 years ago

Hi, thanks for the suggestion! This kind of exists because almost every module has a get_map function (not called get_palette for historical reasons), but the API varies per-module and it's not documented. For example:

In [6]: palettable.wesanderson.get_map('Zissou')
Out[6]: <palettable.wesanderson.wesanderson.WesAndersonMap at 0x103222b38>

In [8]: palettable.colorbrewer.get_map('Blues', 'sequential', 5)
Out[8]: <palettable.colorbrewer.colorbrewer.BrewerMap at 0x109d034a8>

In [12]: palettable.cmocean.sequential.get_map('Turbid_200')
Out[12]: <palettable.cmocean.cmoceanpalette.CmoceanMap at 0x109d27358>

In [13]: palettable.matplotlib.get_map('Viridis_20')
Out[13]: <palettable.matplotlib.matplotlib.MatplotlibMap at 0x109d30128>

(And cubehelix has its own special thing in addition to get_map: http://jiffyclub.github.io/palettable/cubehelix/#make)

One of the challenges with implementing this more generally is that not every palette supports an arbitrary number of colors (really none of them do, but some do up to an upper limit). In particular, qualitative palettes are available for whatever colors are defined and no more. All the colorbrewer palettes max out at 10-12 defined colors, though sequential and diverging ones would probably support interpolation.

All that said, I think is technically possible but it'd require a lot of runtime validation and its usability would hinge on giving the user useful feedback when they ask for something that's not possible to provide. With that in mind, improving discoverability in some other way might be a better use of time. Maybe improvements to the docs or standardizing the get_map function?