For a plotly graph (plotly express scatter, for example), how can I trigger an event/function/callback when I click on an item in the legend of that graph? Specifically, my purpose is to change the colors of the points in the graph by clicking the legend. Thank you in advance!
Asked
Active
Viewed 2,304 times
1 Answers
0
Based on this thread: https://community.plotly.com/t/hide-a-component-with-one-event-and-make-it-visible-with-another/19290 I use the following callback:
@app.callback(Output('your_figure_id', 'figure'),
[Input('your_figure_id', 'restyleData')])
def update(x):
#modify/create new figure as you wish, x contains the legend entry you clicked
return figure
Since the input and output figure id is the same this callback will update the oeiginal figure, if you want to keep the input figure as it is, you just have to give another output id.

Rebeka
- 1
-
Is this just hiding the component, or really removing it from the graph? Because if I have like 20 really heavy components, and I hade 19 of them, will the graph still have the overload of all 20 components since they are just "hidden", or it will have the overhead of only 1 currently visible component? – Petar Mar 25 '21 at 09:26
-
Would you be able to help me create what I asked (so create a graph with points and a legend with a couple of colors - blue, red, green ,yellow, and on each legend click (for example, blue), the colors will turn into blue, and so on?) – Petar Mar 25 '21 at 09:38