How can I update plotly subplots when changing traces?

Viewed 1049

Here's an reproductive example which I desperately need to find a tricky solution for. Using Python and Plotly:

import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np
import plotly.offline as pyo

# Many random traces
d = []
for i in range(0,10):
    d.append(go.Scatter({'y': np.random.randint(0,10,10), 'x': np.array(range(0,11))}))

# One trace solving averages for above traces
t = np.array([i['y'] for i in d])
d2 = np.mean(a=t, axis=0)

trace2 = go.Scatter(x = np.array(range(0,11)), y = d2, xaxis='x2', yaxis='y2', name='averages')

data = d+[trace2]
layout = go.Layout( 
    xaxis1={'anchor': 'y1'},
    yaxis1={'anchor': 'x1', 'domain': [0, .5]},
    xaxis2={'anchor': 'y2'},
    yaxis2={'anchor': 'x2', 'domain': [0.55, 1]}
)
fig = go.Figure(data=data, layout=layout)
pyo.plot(fig)

The problem is when I click on legend's traces to hide the average trace from second subplot is not updating. How to do the trick? How to make it updating without button event - just on pure click on traces in legend? I spent all day yesterday to find a solution and I guess there's no besides triggering button event. But even with button I'd like to know which traces are still visible - it may lead to much more complicated plot than it should be.

I'm going to use it in plotly-dash.

0 Answers
Related