1
凡例にイベントピッキングを作成しましたが、ドラッグ可能な凡例を同時に実装するのが難しいです。イベントピッキングを実装して、この行を削除するときにドラッグ可能な伝説が仕事をするように2つの競合に思える:self.canvas.mpl_connect('pick_event', self.onpick)
イベントピッキング機能を使用している間、matplotlibのドラッグ可能な凡例
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import tkinter as tk
class App:
def __init__(self,master):
self.master = master
aveCR = {0:np.array([.582,1.081,1.507,1.872,2.180]),1:np.array([2.876,6.731,1.132,1.305,1.217])}
legend = ['A', 'AB']
plotFrame = tk.Frame(master)
plotFrame.pack()
f = plt.Figure()
self.ax = f.add_subplot(111)
self.canvas = FigureCanvasTkAgg(f,master=plotFrame)
self.canvas.show()
self.canvas.get_tk_widget().pack()
self.canvas.mpl_connect('pick_event', self.onpick)
# Plot
lines = [0] * len(aveCR)
for i in range(len(aveCR)):
X = range(len(aveCR[i]))
lines[i], = self.ax.plot(X,aveCR[i],label=legend[i])
# Legend
leg = self.ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,ncol=2, borderaxespad=0.)
if leg:
leg.draggable()
self.lined = dict()
for legline, origline in zip(leg.get_lines(), lines):
legline.set_picker(5) # 5 pts tolerance
self.lined[legline] = origline
def onpick(self, event):
# on the pick event, find the orig line corresponding to the
# legend proxy line, and toggle the visibility
legline = event.artist
origline = self.lined[legline]
vis = not origline.get_visible()
origline.set_visible(vis)
# Change the alpha on the line in the legend so we can see what lines
# have been toggled
if vis:
legline.set_alpha(1.0)
else:
legline.set_alpha(0.2)
self.canvas.draw()
root = tk.Tk()
root.title("hem")
app = App(root)
root.mainloop()