jiffyclub / palettable

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

support plotly? #41

Closed ickc closed 3 months ago

ickc commented 2 years ago

Is there any interest in support plotly?

The latest plotly version is v5.4, in an earlier documentation in v3 that doesn't seem to exist now, it demonstrates how to convert from matplotlib color to plotly's.

However https://github.com/hackingmaterials/matminer/issues/232#issuecomment-395620857 mentioned

converting matplotlib color maps into Plotly-capable format doesn't work outside of the few examples Plotly provides

without much details or even mention if it is the method outlined in the page above. So may be it is not so easy to support it.

jiffyclub commented 2 years ago

Would be great to support if we can figure out how! Looks like there is some info under here: https://plotly.com/python-api-reference/generated/plotly.colors.html#module-plotly.colors

From poking around in there it looks like plotly takes colors as lists of hex strings (e.g. '#ae7eb0'). You can see that also in their source code: https://github.com/plotly/plotly.py/blob/master/packages/python/plotly/_plotly_utils/colors/sequential.py

The good news is that Palettable already supports that format! Use the .hex_colors attribute on a palette to get exactly that:

In [1]: import palettable

In [2]: palettable.cmocean.sequential.Algae_10
Out[2]: <palettable.cmocean.cmoceanpalette.CmoceanMap at 0x10e0e7ac0>

In [3]: palettable.cmocean.sequential.Algae_10.hex_colors
Out[3]:
['#D7F9D0',
 '#B4E0A8',
 '#8EC982',
 '#64B463',
 '#2CA052',
 '#07874D',
 '#126E45',
 '#195538',
 '#193C27',
 '#122414']

If you could try that out and let me know if it works it'd be much appreciated.

It also looks like there's significant overlap between Palettable's colormaps and Plotly's built-in ones. They are listed here and in some package specific submodules (e.g. Carto and cmocean):

ickc commented 2 years ago

Thanks! I will test probably after this week and report back.

ickc commented 2 years ago

@jiffyclub, thanks! I tested this and it works with plotly. #42 documents this.