Как использовать Legend Picking с matplotlib в файле python?
Я создал API с помощью django для визуализации графиков
.
Я подключился к базе данных и хочу добавить в график анимацию выбора легенды, но что-то не так, кто-нибудь может меня поправить?
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)
plt.legend()
legend = plt.legend()
legend.set_picker(True)
def on_pick(event):
legline = event.artist
origline = lined[legline]
visible = not origline.get_visible()
origline.set_visible(visible)
legline.set_alpha(1.0 if visible else 0.2)
fig.canvas.draw()
fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()
```