How using Legend Picking with matplotlib on python file?

I created an API using django for plot visualisation
I am connected to my database and i would like to add to my plot this animation of legend picking , but something wrong , someone can correcting me ?

def one (df_a, a, b, c=2):
    X = df_a.iloc[:, a:b].to_numpy()
    l = X.shape[0]
    D = np.zeros((l, l))
    idx = range(len(X))
    for j in range(1, X.shape[0]):
        A = X
        D[j, idx] = np.abs(A).max(1)
    d = D.sum(0) / l

    diff = np.sort(d)[1:] - np.sort(d)[:-1]
    if np.where(diff > c * np.median(diff))[0].size != 0:
        t = np.sort(d)[np.where(diff > c * np.median(diff))[0] + 1].min()
        return df_a.iloc[np.where(d >= t)].index

def two (df_a, a, b, c):
    outliers = one(df_a, a, b, c)
    colors = 'red'
    plt.figure(figsize=(20, 10))
    plt.xlabel('pi ---> ')
    plt.ylabel('ab ---> ')
    plt.plot(df_a.to_numpy()[:, 1:].T, 'blue')
    for i, outlier in enumerate(outliers):
        plt.plot(df_abs.loc[outlier].iloc[1:], c=colors[i], label=outlier)
    legend = plt.legend()

def on_pick(event):
    legline = event.artist
    origline = lined[legline]
    visible = not origline.get_visible()
    legline.set_alpha(1.0 if visible else 0.2)
    fig.canvas.mpl_connect('pick_event', on_pick)

Back to Top