FigureWidget Histogram Click event

I am trying to see if it’s possible to use on_click with a histogram trace (with clickmode=‘select’), to get the id of the histogram bin selected and (even better) the indices of the points in the bin selected. This doesn’t seem to work in the example below, the points_inds field is empty. NB: this works with dragmode=‘select’, but not with clickmode=‘select’.
Any help appreciated (indentation lost when copying and pasting code).

import pandas as pd
import numpy as np
import plotly.graph_objects as go
from ipywidgets import widgets

col1=pd.Series([‘A’,‘B’,‘A’,‘A’,‘C’,‘B’,‘B’,‘B’,‘D’,‘C’])

fig = go.FigureWidget(
data=[
go.Histogram(x=col1, marker={‘color’: ‘blue’},
selected={‘marker’: {‘color’: ‘firebrick’}}, unselected={‘marker’: {‘opacity’: 0.3,‘color’:‘blue’}}
)
])

fig.update_layout(
height=400,
yaxis={‘title’: ‘Count’, ‘domain’: [0, 1]},
clickmode=‘select’,hovermode=‘closest’)

def update_color(trace, points, state):
# Update histogram plot selection
fig.data[0].selectedpoints = points.point_inds

fig.data[0].on_click(update_color)

fig

fig.data[0].selectedpoints # returns nothing with ‘clickmode’, returns item indices in selected bin as expected, with ‘dragmode’ (and ‘on_selection’ instead of ‘on_click’ in the callback registration)

1 Like