在多个子图上垂直光标跟随鼠标



我正在为个人使用编写一个小脚本,该脚本旨在形成一些数据记录,以便能够分析它们。这个想法是选择我想要看到的数据,然后检查它们,它们将显示在多个子地段。我正在使用Python与tkinter和matplotlib。

我目前的问题是,我想有一个垂直光标跟随鼠标移动,我希望这个光标是相同的所有显示的子图。我试着用"多光标"从matplotlib,但它不工作,我没有错误。我认为问题是我给MultiCursor作为参数的子图列表,但在我的代码中,我不知道如何通过其他方式做到这一点。

如有任何意见或评论,不胜感激。

最小可复制代码:

import tkinter as tk
from matplotlib.widgets import MultiCursor
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
w_main = tk.Tk()
w_main.geometry("1080x720")
f_leftFrame = tk.Frame(w_main)
f_leftFrame.pack(side=tk.LEFT, anchor='nw', fill=tk.BOTH)
f_graphFrame = tk.Frame(w_main)
f_graphFrame.pack(fill=tk.BOTH, expand=1)
fig = Figure(figsize=(5, 5), dpi=100)
graph = FigureCanvasTkAgg(fig, master=f_graphFrame)
graph.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
time = [0, 0.150, 0.3, 0.450, 0.6, 0.750, 0.9]
record1 = [0, 5, 3, 6, 2, 8, 9]
record2 = [1, 6, 8, 5, 7, 2, 5]
record3 = [9, 7, 5, 3, 0, 0, 2]
labels = ["record1", "record2", "record3"]
dic_state_cb = {}
test_list = []
def curveBuilder():
dic_state_cb = {}
sel_curves = []
global test_list
for i in labels:
dic_state_cb["state_{}".format(i)] = globals()["state_{}".format(i)].get()
if dic_state_cb["state_{}".format(i)] == 1:
sel_curves.append(i)
for i in range(len(labels)):
try:
globals()["ax{}".format(i)].remove()
test_list = []
except:
pass
for i in range(len(sel_curves)):
globals()["ax{}".format(i)] = fig.add_subplot(int("{}1{}".format(len(sel_curves), i+1)))
test_list.append(globals()["ax{}".format(i)])
x = time
if len(sel_curves) > 0:
for i in range(len(sel_curves)):
curve = globals()[sel_curves[i]]
test_list[i].plot(x, curve)
test_list[i].set_ylabel(sel_curves[i])
if i < len(sel_curves) - 1:
x_axis = test_list[i].axes.get_xaxis()
x_axis.set_visible(False)
multi = MultiCursor(graph, test_list, color="'r'", lw=2.0, vertOn=True)
graph.draw()
for i in enumerate(labels):
globals()["state_{}".format(i[1])] = tk.IntVar()
cb = tk.Checkbutton(f_leftFrame, text=i[1], variable=globals()["state_{}".format(i[1])], command=curveBuilder,
anchor=tk.W)
cb.pack(fill=tk.BOTH)
tk.mainloop()

我终于自己找到了答案。问题来自包含MultiCursor调用结果的变量,该结果在我的代码中是函数的局部。如果我将它声明为全局变量,那么它就可以正常工作。

我不知道为什么,但它解决了问题。我希望知道原因的人能告诉我答案。

下面是工作代码:

import tkinter as tk
from matplotlib.widgets import MultiCursor
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
w_main = tk.Tk()
w_main.geometry("1080x720")
f_leftFrame = tk.Frame(w_main)
f_leftFrame.pack(side=tk.LEFT, anchor='nw', fill=tk.BOTH)
f_graphFrame = tk.Frame(w_main)
f_graphFrame.pack(fill=tk.BOTH, expand=1)
fig = Figure(figsize=(5, 5), dpi=100)
graph = FigureCanvasTkAgg(fig, master=f_graphFrame)
graph.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
time = [0, 0.150, 0.3, 0.450, 0.6, 0.750, 0.9]
record1 = [0, 5, 3, 6, 2, 8, 9]
record2 = [1, 6, 8, 5, 7, 2, 5]
record3 = [9, 7, 5, 3, 0, 0, 2]
labels = ["record1", "record2", "record3"]
dic_state_cb = {}
test_list = []
def curveBuilder():
dic_state_cb = {}
sel_curves = []
global test_list
for i in labels:
dic_state_cb["state_{}".format(i)] = globals()["state_{}".format(i)].get()
if dic_state_cb["state_{}".format(i)] == 1:
sel_curves.append(i)
for i in range(len(labels)):
try:
globals()["ax{}".format(i)].remove()
test_list = []
except:
pass
for i in range(len(sel_curves)):
globals()["ax{}".format(i)] = fig.add_subplot(int("{}1{}".format(len(sel_curves), i+1)))
test_list.append(globals()["ax{}".format(i)])
x = time
if len(sel_curves) > 0:
for i in range(len(sel_curves)):
curve = globals()[sel_curves[i]]
test_list[i].plot(x, curve)
test_list[i].set_ylabel(sel_curves[i])
if i < len(sel_curves) - 1:
x_axis = test_list[i].axes.get_xaxis()
x_axis.set_visible(False)
global multi
multi = MultiCursor(graph, test_list, color='r', lw=2.0, vertOn=True)
graph.draw()
for i in enumerate(labels):
globals()["state_{}".format(i[1])] = tk.IntVar()
cb = tk.Checkbutton(f_leftFrame, text=i[1], variable=globals()["state_{}".format(i[1])], command=curveBuilder,
anchor=tk.W)
cb.pack(fill=tk.BOTH)
tk.mainloop()