Interactive Graph Click Event

#1

I’m trying to figure out if it’s possible to create a PlotLy Pie Chart in python with an on_click event in JupyterLab. I’ve been able to run the scatter plot example:

When I try to adapt this for a pie chart, though, the callback doesn’t look like it’s ever being called. I’m not sure if this is possible and there’s something missing in my adaptation, or if it’s a capability that’s only available for scatter and not for other kinds of plots.

Can anyone advise or provide feedback on this? Thanks!

#2

Hi @tdonadio,

Click events on pie charts should work. Here’s an example that’s working for me

import plotly.graph_objs as go
from ipywidgets import Output, VBox

fig = go.FigureWidget()
pie = fig.add_pie(values=[1, 2, 3])

out = Output()
@out.capture(clear_output=True)
def handle_click(trace, points, state):
    print(points.point_inds)

pie.on_click(handle_click)

VBox([fig, out])

pie_click

Hope that helps!
-Jon

1 Like
#3

Thanks! Yes, that works for me as well. And seeing a successful example allowed me to identify what what I was doing to adapt the scatterplot example that wasn’t working.

I noticed two things that don’t work following the scatterplot example (that is, it will create plots, but the callback won’t fire on click):

  • iplot won’t work for a callback-based interactive pie chart (or at least, it doesn’t when I change the example only to use iplot rather than VBox).
  • it won’t work if I create the “pie” object with go.Pie instead of with fig.add_pie (as in: fig = go.FigureWidget([go.Pie(labels=labels, values=values)])).

I can move forward now with a working example, but I’m curious to understand these differences a bit better. Can someone help to explain the reason(s) for them? I’m new to using PlotLy, so it may just be that this isn’t clear to me because of my lack of familiarity with the programming model. Thanks!

#4

Hi @tdonadio,

Glad you got it working!

iplot won’t work for a callback-based interactive pie chart (or at least, it doesn’t when I change the example only to use iplot rather than VBox).

This is correct, when a FigureWidget is displayed using iplot, the resulting view is not an ipywidget and so the events can’t be synced back to the Python side. A FigureWidget should be allowed to display itself (by being the last expression in an input cell), or it can be displayed using the ipywidgets.display method (See https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Basics.html#display()).

it won’t work if I create the “pie” object with go.Pie instead of with fig.add_pie (as in: fig = go.FigureWidget([go.Pie(labels=labels, values=values)])).

What’s happening here is that traces are copied when they are passed to figure constructors. So you do need to add a trace to the figure and then access it in order for the callbacks to be synced up. As an alternative to using the add_* methods, you can retrieve the trace from the figure’s data property. E.g.

fig = go.FigureWidget([go.Pie(labels=labels, values=values)]))
pie = fig.data[0]

Hope that helps clear things up a bit!
-Jon

#5

Hi, is there an example of click and select points in 3D scatter? !

I try to let “dragmode”:‘select’, but this method is not suitable for 3D scatter.

#6

Hi @visual,

Unfortunately 3D point selection isn’t supported yet. See https://github.com/plotly/plotly.js/issues/3511 for initial discussion.

-Jon