这是我在tkinter中关于Matplotlib功能的另一个question的后续内容。我正在尝试开发一个程序,当我训练一个模型时,它会打开多个包含绘图的窗口。值作为字典存储在aveCR中,每个键存储多个数组。对于每个键,我在一个新窗口中绘制数组,因此使用For循环打开一个新窗口(不确定这是否是一个好的做法!)你知道吗
我的问题是每个窗口的图例。打开/关闭线条的功能只在最后一个窗口上可用,我希望在每个打开的新窗口上都可以使用此功能。我知道self.lined
被for循环的最终绘图覆盖,但我不确定是否应该将其添加到for循环。你知道吗
我在下面为aveCR添加了虚拟图,这样代码就可以运行了。任何建议在接近这将不胜感激!你知道吗
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
# Frame for the training values
buttonFrame = tk.Frame(master)
buttonFrame.pack()
self.buttonGenerate = tk.Button(master=buttonFrame,
text='Train',
command=self.train)
self.buttonGenerate.grid(column=2,row=0)
def train(self):
aveCR = {0:{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])},
1:{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 = {0: ['A', 'AB'], 1: ['A', 'AB']}
for i in range(len(aveCR)):
t = tk.Toplevel(self.master)
# Frame for the plot
plotFrame = tk.Frame(t)
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[i])
for j in range(len(aveCR[i])):
X = range(0,len(aveCR[i][j]))
lines[j], = self.ax.plot(X,aveCR[i][j],label=legend[i][j])
leg = self.ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,ncol=2, borderaxespad=0.)
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()
这可能主要是一个设计问题。我建议为每个绘图窗口使用一个类。
App
类可以根据需要实例化任意多个绘图窗口,并将相应的数据作为参数提供。每个绘图窗口都为自己管理图例和事件。你知道吗相关问题 更多 >
编程相关推荐