Jun-22-2020, 01:57 PM
Hello
I´ve written the code below, which creates a figure with two subplots. Each subplot has a "draggable line" that returns the x-value when released. Is it possible to implement this draggable line in a GUI using a framework such as Tkinter or PyQT? So far, I´ve only managed embed the plots and lines in a GUI but the "Interactive" part is lost.
Any help is greatly appreciated
I´ve written the code below, which creates a figure with two subplots. Each subplot has a "draggable line" that returns the x-value when released. Is it possible to implement this draggable line in a GUI using a framework such as Tkinter or PyQT? So far, I´ve only managed embed the plots and lines in a GUI but the "Interactive" part is lost.
Any help is greatly appreciated
import matplotlib.pyplot as plt import matplotlib.lines as lines import numpy as np import random class draggableline: def __init__(self, ax, XorY): self.ax = ax self.c = ax.get_figure().canvas self.XorY = XorY x = [XorY, XorY] y = [-1, 10] self.line = lines.Line2D(x, y, color='red', picker=5) self.ax.add_line(self.line) self.c.draw_idle() self.sid = self.c.mpl_connect('pick_event', self.clickonline) def clickonline(self, event): if event.artist == self.line: self.follower = self.c.mpl_connect("motion_notify_event", self.followmouse) self.releaser = self.c.mpl_connect("button_press_event", self.releaseonclick) def followmouse(self, event): self.line.set_xdata([event.xdata, event.xdata]) self.c.draw_idle() def releaseonclick(self, event): self.XorY = self.line.get_xdata()[0] print (self.XorY) self.c.mpl_disconnect(self.releaser) self.c.mpl_disconnect(self.follower) class draggableline2: def __init__(self, ax2, XorY): self.ax2 = ax2 self.c = ax2.get_figure().canvas self.XorY = XorY x = [XorY, XorY] y = [-1, 10] self.line = lines.Line2D(x, y, color='red', picker=5) self.ax2.add_line(self.line) self.c.draw_idle() self.sid = self.c.mpl_connect('pick_event', self.clickonline) def clickonline(self, event): if event.artist == self.line: self.follower = self.c.mpl_connect("motion_notify_event", self.followmouse) self.releaser = self.c.mpl_connect("button_press_event", self.releaseonclick) def followmouse(self, event): self.line.set_xdata([event.xdata, event.xdata]) self.c.draw_idle() def releaseonclick(self, event): self.XorY = self.line.get_xdata()[0] print (self.XorY) self.c.mpl_disconnect(self.releaser) self.c.mpl_disconnect(self.follower) fig, (ax, ax2) = plt.subplots(2) ax.plot([0, 1, 2, 3, 4, 5], [1, 1, 6, 4, 6, 2]) ax2.plot([0, 1, 2, 3, 4, 5], [1, 1, 2, 4, 2, 1]) Tline = draggableline(ax, 0.1) Tline2 = draggableline2(ax2, 0.1) plt.show()