【问题标题】:Matplotlib/tkinter: Event picking on the legendMatplotlib/tkinter:图例上的事件选择
【发布时间】:2018-03-25 10:45:19
【问题描述】:

我无法修改此代码,因此可以通过单击图例在 tkinter 中选择一行。我想从这个example修改代码

这是迄今为止我想出的代码。它只是在 tkinter 中绘制线条。

import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

class Main:
    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        plotFrame = tk.Frame(master)
        plotFrame.pack()

        f = Figure(figsize=(5,4),dpi=100)
        self.ax = f.add_subplot(111)
        self.canvas = FigureCanvasTkAgg(f,master=plotFrame)
        self.canvas.show()
        self.canvas.get_tk_widget().pack()

        line1, = self.ax.plot(t, y1, lw=2, color='red', label='1 HZ')
        line2, = self.ax.plot(t, y2, lw=2, color='blue', label='2 HZ')
        leg = self.ax.legend(loc='upper left', fancybox=True, shadow=True)
        leg.get_frame().set_alpha(0.4)

    def onpick(event):
        # on the pick event, find the orig line corresponding to the
        # legend proxy line, and toggle the visibility
        legline = event.artist
        origline = 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)
        fig.canvas.draw()

    fig.canvas.mpl_connect('pick_event', onpick)

    plt.show()



root = tk.Tk()
my_gui = Main(root)
root.mainloop()

【问题讨论】:

  • 我不明白这个问题。在您链接到的示例中,有一个函数on_pick 和一个'pick_event' 注册到它。由于您的代码中完全缺少这些部分,因此它当然不会显示出预期的效果。您需要包含它们,如果您遇到问题,请显示重现此问题的代码。
  • 我已经编辑了代码以显示我会做什么。如果代码是从 python 运行的,则可以打开和关闭这些行。如果我运行它,则无法在 tkinter 窗口中打开或关闭这些行。
  • 注意缩进。还有> 5个未定义的变量。在问题中发布代码后,验证它是否可以被复制并运行以重现您所询问的(不)期望的行为。

标签: python matplotlib tkinter


【解决方案1】:

为了写你的tkinter GUI in an object-oriented style,你需要更好地利用面向对象的结构。特别是:

1- 将您的代码保留在类的方法中:

# In your example, the following lines are outside any method
fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

2-使用self变量来存储你想用不同方法访问的对象:

# Add self as the first argument in onpick()
def onpick(self, event):
    (...)
    # Now you can access self.canvas defined in __init__()
    self.canvas.draw()

请参阅下面的代码的工作版本:

import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np

t = np.arange(0.0, 0.2, 0.1)
y1 = 2*np.sin(2*np.pi*t)
y2 = 4*np.sin(2*np.pi*2*t)

class Main:
    def __init__(self, master):
        self.master = master
        master.title("A simple GUI")

        plotFrame = tk.Frame(master)
        plotFrame.pack()

        f = Figure(figsize=(5,4),dpi=100)
        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)

        line1, = self.ax.plot(t, y1, lw=2, color='red', label='1 HZ')
        line2, = self.ax.plot(t, y2, lw=2, color='blue', label='2 HZ')
        leg = self.ax.legend(loc='upper left', fancybox=True, shadow=True)
        leg.get_frame().set_alpha(0.4)

        # we will set up a dict mapping legend line to orig line, and enable
        # picking on the legend line
        lines = [line1, line2]
        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()
my_gui = Main(root)
root.mainloop()

【讨论】:

  • 感谢乔瑟林的帮助!非常感谢您的建议
猜你喜欢
  • 2018-03-31
  • 1970-01-01
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 1970-01-01
  • 2021-12-04
相关资源
最近更新 更多