Dynamic switch of dimension used for color

Hello,

My dataset is a dataframe with each observation having ‘x’ ‘y’ coordinates, and a bunch of measures as additional columns.
I would like to plot these points and dynamically select which measure is used for coloring them.

I tried to update the dimension name used for coloring with apply.opts:

import numpy as np
import pandas as pd
import panel as pn
import holoviews as hv
from holoviews import opts
pn.extension()

dataset = pd.DataFrame({"x": [-1,1,2]*3,
                        "y": [-1]*3 + [3]*3 + [4]*3,
                        "val1": np.random.choice(100, size=9),
                        "val2": range(0,9)})

# Prepare the points representation of dataset
data_points = hv.Points(dataset,
                        vdims=["val1", "val2"])

# Create Panel selectors for color source and mapping
select_list_colorsource = pn.widgets.Select(name='Color source', 
                                            groups={'Measures':list(dataset.iloc[:,2:].columns)}) # Get data columns names except coordinates ones

select_list_colormap = pn.widgets.Select(name='Color map', options=["RdBu", "PiYG"])

# Dynamically set color source an mapping from selectors param
data_points=data_points.apply.opts(color=select_list_colorsource.param.value, 
                                   cmap=select_list_colormap.param.value)

pn.Row(data_points.opts(size=30, colorbar=True), 
       pn.Column( select_list_colorsource, 
                 select_list_colormap))

While the switching occurs, it seems that the color source is updated (colorbar updates) but the points don’t get updated with appropriate colors.
During tests I also noted that when specifying an additional option clim (either programmatically or with a slider via apply.opts), there is no switching at all.

Could you help me to find a working solution ?

I also wonder how this strategy compares with a Dytnamic map approach (or gridded dataset ?).
I could not find the way to code this. Data should be reformatted to a multidmensional array for proper slicing ?

Thank you for your comments.

-----
holoviews           1.15.4
numpy               1.23.5
pandas              1.5.3
panel               0.14.3
session_info        1.0.0
-----
IPython             8.9.0
jupyter_client      8.0.2
jupyter_core        5.2.0
jupyterlab          3.4.8
notebook            6.4.12
-----
Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:56:21) [GCC 10.3.0]
Linux-5.4.0-132-generic-x86_64-with-glibc2.35
-----

Hi @rfenouil

I tried your code and it is not clear to me what does not work. When I interact with the app it works like below ??

coloring

import numpy as np
import pandas as pd
import panel as pn
import holoviews as hv
from holoviews import opts
pn.extension()

dataset = pd.DataFrame({"x": [-1,1,2]*3,
                        "y": [-1]*3 + [3]*3 + [4]*3,
                        "val1": np.random.choice(100, size=9),
                        "val2": range(0,9)})

# Prepare the points representation of dataset
data_points = hv.Points(dataset,
                        vdims=["val1", "val2"])

# Create Panel selectors for color source and mapping
select_list_colorsource = pn.widgets.Select(name='Color source', 
                                            groups={'Measures':list(dataset.iloc[:,2:].columns)}) # Get data columns names except coordinates ones

select_list_colormap = pn.widgets.Select(name='Color map', options=["RdBu", "PiYG"])

# Dynamically set color source an mapping from selectors param
data_points=data_points.apply.opts(color=select_list_colorsource.param.value, 
                                   cmap=select_list_colormap.param.value)

pn.Row(data_points.opts(size=30, colorbar=True), 
       pn.Column( select_list_colorsource, 
                 select_list_colormap)).servable()
panel serve script.py

Hi @Marc,

Thank you for testing it.
The color scale changes as expected, but the points don’t seem to get a correct color when switching to “val2”.
With the declaration "val2": range(0,9) I expected the points to cover the full range of the color scale, does it sound correct ?

I opened an issue here: Updating color source with apply.opts does not map points colors correctly · Issue #5652 · holoviz/holoviews · GitHub