How to change face_color in a points layer with a slider?

Hi everybody,

I need to change the values of face_color property in a points layer with a slider. At the moment, this is a code bit in my jupyter notebook:

i = 0
point_properties = {
    'confidence': data_t[i],
}
viewer.add_points(data, name='data', size=3)
viewer.layers['data'].properties = point_properties
viewer.layers['data'].face_color = 'confidence'
viewer.layers['data'].face_colormap = 'viridis'

I want to choose ‘i’ (index of data_t) in order to change the color of the points. Before adding a slider would be good to do it programmatically. I tried changing point_properties and executing viewer.layers[‘data’].properties again, but it doesn’t work.

In my whole application, I already have a slider because I use a surface layer with time-series data (the slider is auto-generated), so I would need to catch the callback to change the index of data_t and “refresh” the view.

Any suggestion?

Hi @scaracciolo

Does that time slider correspond exactly to the number of colors that you have? And does the position of that slider determine the color? If so then agreed, hooking into a slider callback seems like a good approach for changing color. You can do this by listening to the viewer.dims.events.axis event and using the viewer.dims.point variable.

If not and the colors and the timeseries slider are independent then you might just want to add a new slider to update color independently. For that you might want to use magicgui, see this example of a custom parameter sweep https://magicgui.readthedocs.io/en/latest/examples/napari_parameter_sweep/

Let us know how you get on! We can help more if you run into problems

Thank you very much @sofroniewn,

I share the solution to change the color of the points with a slider auto-generated by the surface layer, maybe it can be useful to other people.

viewer = napari.Viewer(ndisplay=3)
viewer.add_surface((nodos, caras, data1_t), name='superficie epi')
viewer.layers['superficie epi'].colormap = 'viridis'
viewer.add_points(electrodos, name='electrodos epi', size=3)
viewer.layers['electrodos epi'].blending = 'additive'
viewer.layers['electrodos epi'].opacity = .5
viewer.layers['electrodos epi'].n_dimensional = True
viewer.layers['electrodos epi'].face_colormap = 'viridis'

point_properties = {
    'confidence': data2_t[0],
}
viewer.layers['electrodos epi'].properties = point_properties
viewer.layers['electrodos epi'].face_color = 'confidence'

def callback(e):
    index = e.value
    point_properties['confidence'] = data2_t [index] # 
    viewer.layers['electrodos epi'].face_color = 'confidence' # l'inea necesaria para actualizar la vista

viewer.dims.events.axis.connect(callback)

Demo