Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Annotation visibility toggle by clicking trace on legend

Hello

As the title implies, is there a way to make an annotation dissapear/appear by clicking on the corresponding trace in the legend of a plotly chart?

Suppose I have a scatterplot with a nonlinear trendline I added with add_trace. The trendline has an entry in the legend box and I want to display/hide the equation for the trendline when I toggle the trace on/off in the legend box.

The annotations have a visible property that can be accessed, and I read about the layout.legend.itemclick but it only covers the events of the affected trace, no option to include an object like an annotation.

Any way to do this? Thank you beforehand.

Hi @PL_PUAR welcome to the forum! The easiest thing to do would be to make the equation part of the trace by using the text argument of a go.Scatter trace (you can add it to the first or the middle data point and the other items can just be empty strings).

That’s a very clever workaround. Thank you @Emmanuelle!

I need to test the solution proposed since I can visualize 2 issues with it.

  1. The text is tied to a (x,y) coordinate and would get lost of the screen by zooming and panning across the plot
  2. Being tied to an (x,y) coordinate it can’t be freely placed on the plot.

Gonna try and see if I can make it work, I’m new to plotly and python… If I need further help I’ll post here again.

Thanks again

Hi Emmanuelle,

Would you be able to provide an example of what the solution would look like? I’m trying to graph two separate things in a scatterplot now and, while clicking on the legend will toggle the markers on and off properly, it doesn’t toggle the associated annotations off and on.

Thanks!

Doug