lets-plot icon indicating copy to clipboard operation
lets-plot copied to clipboard

Request for new function : Matplotlib colormap format

Open egayer opened this issue 1 year ago • 1 comments

Hi guys,

While the color scale functions in lets-plot are awesome, I would like to propose adding a function that can read any colormap given in the Matplotlib format. This would be different from ..._color_brewer() and ..._color_viridis() as it would take the name of the colormap imported from another package as an argument.

Several packages exist for scientific colormaps (cmcrameri, CMOcean, etc., see this article) and most of them are designed so colormaps can be directly used with Matplotlib.

In order to use such colormaps with lets-plot, I wrote a small function (ggcmap2hex()) that translates colormaps (in Matplotlib format) into a list of hex codes. The list can then be used in the scale_xxxx_gradientn() function in lets-plot (xxxx being either fill or color).

Currently, this function relies on Matplotlib, which makes it dependent on Matplotlib and on its versions. Which is not what you want but I couldn't code it natively for lets-plot :$

It would be super cool if you guys could implement such a function to be used in scale_xxxx_gradientn(). Or to create a new function called something like scale_xxxx_colormap() or scale_xxxx_cmap() that would do scale_xxxx_gradientn(colors=ggcmap2hex(cm.jet)) such as :

from matplotlib import cm
...
...
 + scale_color_cmap(cm.turbo)

or

import cmcrameri.cm as cmc
...
...
 + scale_fill_cmap(cmc.batlow)

I attached a notebook that contains ggcmap2hex() and a demo. ggcmap2hex.zip

egayer avatar Jun 01 '24 17:06 egayer

Thanx! this is interesting, certainly worth considering.

alshan avatar Jun 04 '24 17:06 alshan

Coming in the next release soon: https://nbviewer.org/github/JetBrains/lets-plot/blob/master/docs/f-24e/scale_cmapmpl.ipynb

alshan avatar Jul 25 '24 21:07 alshan

Awesome ! it looks great, thanks for considering the idea !

egayer avatar Jul 26 '24 06:07 egayer

Hi @egayer, v4.4.0 is out, but beware of an unfortunate issue that we failed to detect in time and it made its way into this release.

alshan avatar Aug 02 '24 21:08 alshan