Description
I am working in a Jupyter notebook and when accessing the selectedpoints
attribute of a go.Histogram
or the points list in the on_selection
callback, the list does not contain integer indices of the selected points but instead just a list of objects.
import plotly.graph_objects as go
import numpy as np
np.random.seed(1)
x = np.random.randn(500)
def on_selection_print(obj, points, selector):
print(points.point_inds)
figure_widget = go.FigureWidget(data=[go.Histogram(x=x)])
figure_widget.update_layout(dragmode="select")
figure_widget.data[0].on_selection(on_selection_print)
figure_widget
When selecting points in the histogram, the output looks something like:
[<object object at 0x7ff062c497f0>, <object object at 0x7ff062c497f0>, <object object at 0x7ff062c497f0>, <object object at 0x7ff062c497f0>, <object object at 0x7ff062c497f0>, <object object at 0x7ff062c497f0>]
A call to figure_widget.data[0].selectedpoints
returns the same list.
The expected output would be something like:
[0, 12, 13, 26, 31, ..., 2534]
As you can see, all objects are identical (memory address 0x7ff062c497f0
) and the objects in the list do not have any attributes. The length of the list corresponds to the number of bars selected if that helps.
I am working with plotly version 4.9.0
, ipywidgets version 7.5.1
and notebook version 6.0.1
.
UPDATE:
I checked how Plotly.js handles selection for histograms and found that it creates an object for each selected bar, which in turn contains all the points for that bar.
e.g.
{
points: [
0: {..., pointIndices: [1,2,3,4], ...},
1: {..., pointIndices: [5,6,7,8,9], ...},
...
],
range: [...]
}
Codepen can be found here: https://codepen.io/meffmadd/pen/zYqGmaw
So the problem seems to be that the points are not correctly serialised for histograms. Without understanding the whole codebase I think there seem to be two options:
- Update the setter to
_js2py_pointsCallback
when the figure is a histogram in - Updating the model for the histogram on the Python side to reflect the behaviour of the JavaScript implementation. I don't know how exactly this would work yet.
Any pointers in the right direction are highly appreciated!