✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

List index out of range error from scatter hover function

I am trying to create a hover function for a Plotly graph object FigureWidget, similar to the cars_exploration example notebook by @jonmmease.

Based on the error message I get it seems that I am not defining the appropriate scatter variable. Is there any hints in these error messages where I have done a mistake?

In the following code fig_mapper is a FigureWidget with two go.Scatter() data entries data=[trace1, trace2].

scatter = fig_mapper.data[0]
scatter

The above code provides the following output.

Scatter({
    'hoverinfo': 'none',
    'line': {'color': '#888', 'width': 1},
    'mode': 'lines',
    'uid': 'd45ef4cd-8a27-4a56-b848-459844d22515',
    'x': [3.131244834512655, 2.5605970575666706, None, ..., -0.27044321387309567,
          -0.8346024911912975, None],
    'y': [3.8405470297671713, 3.8907432081494733, None, ..., -2.7660395505627715,
          -2.9918234972840496, None]
})

My test hover function is this.

node_stats = HTML()
def hover_node_stats(trace, points, state):
    ind = points.point_inds[0]
    df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
    node_stats.value = df.to_html()

scatter.on_hover(hover_node_stats) 

Hovering over a node in the figure results in this error.

<ipython-input-24-09cb05ac1718> in hover_node_stats(trace, points, state)
      1 node_stats = HTML()
      2 def hover_node_stats(trace, points, state):
----> 3     ind = points.point_inds[0]
      4     df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
      5     node_stats.value = df.to_html()

IndexError: list index out of range

I solved the problem by changing

ind = points.point_inds[0]

to

ind = points.point_inds

in the hover_node_stats function.

Why did this work?