Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The selectedpoints attribute for Histogram plots is being set incorrectly #2698

Closed
meffmadd opened this issue Aug 10, 2020 · 2 comments · Fixed by #2711
Closed

The selectedpoints attribute for Histogram plots is being set incorrectly #2698

meffmadd opened this issue Aug 10, 2020 · 2 comments · Fixed by #2711

Comments

@meffmadd
Copy link
Contributor

meffmadd commented Aug 10, 2020

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:

  1. Update the setter to _js2py_pointsCallback when the figure is a histogram in
    this.model.set("_js2py_pointsCallback", pointsMsg);
  2. 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!

@meffmadd meffmadd changed the title The selectedpoints attribute in histogram does not contain points The selectedpoints attribute for Histogram plots is being set incorrectly Aug 10, 2020
@meffmadd
Copy link
Contributor Author

After some digging I found that buildPointsObject accesses the incorrect key. Normally all the points are in a field named pointNumber, however, the selection for the histogram contains pointNumbers which are lists corresponding to the selected bars instead of single values. Since the attribute pointNumber does not exist for Histograms it is undefined which is then passed in the message.

buildPointsObject: function (data) {

In order to resolve the issue I had to flatten the pointNumbers arrays to a single array to conform to the behaviour on the Python side since passing the nested point numbers caused issues with highlighting the selection.

I will shortly create a pull request for the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants