python - 我的 GUI 上的可拖动线和绘图 Matplotlib 之间的交点
问题描述
我有一个情节和一条可拖动的线。我想在空闲和移动时标记线和绘图的交叉点,交叉点可以是任何标记。
可拖动线的代码:
class draggable_lines:
def __init__(self, ax, kind, XorY):
self.ax = ax
self.c = ax.get_figure().canvas
self.o = kind
self.XorY = XorY
if kind == "h":
x = [-1, 1]
y = [XorY, XorY]
elif kind == "v":
x = [XorY, XorY]
y = [0, 5000000]
self.line = lines.Line2D(x, y, 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:
print("line selected ", event.artist)
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):
if self.o == "h":
self.line.set_ydata([event.ydata, event.ydata])
else:
self.line.set_xdata([event.xdata, event.xdata])
self.c.draw_idle()
def releaseonclick(self, event):
if self.o == "h":
self.XorY = self.line.get_ydata()[0]
else:
self.XorY = self.line.get_xdata()[0]
print (self.XorY)
self.c.mpl_disconnect(self.releaser)
self.c.mpl_disconnect(self.follower)
以及用于 GUI 的绘图代码
figure = Figure(figsize=(5, 4), dpi=100)
plot = figure.add_subplot(1, 1, 1)
figure.suptitle("Date", fontsize=12)
Line, = plot.plot(x, y, color="Blue",linewidth = LineWidth)
canvas = FigureCanvasTkAgg(figure, root)
canvas.get_tk_widget().place(x=10,y=200)
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
toolbar.update()
toolbar.place(x=10,y=600)
解决方案
推荐阅读
- javascript - 如何在 Angular 9 中正确安装预建模板
- entity-framework-core - EntityFrameworkCore - 指定多个属性时
- java - 我怎么能在 Kruskal 的算法中以字符串的形式给出位置(顶点)的名称,更准确地说是城市名称?
- kotlin - 为什么在 Kotlin 中有新数据时会自动删除房间中的数据
- postgresql - 从 Snowflake 查询 PostgreSQL 数据库
- uiviewcontroller - 我应该在 SwiftUI 中的 updateUIViewController(_:context:) 内调用 viewDidLoad()
- python - 将 sql server 的 Datetimeoffet 数据类型转换为在 python 数据框中使用
- javascript - 如何在没有Phonegap的情况下在android中运行JavaScript文件?
- node.js - GCloud Vision API 权限在第二次请求时被拒绝
- php - 如何将整个列从数据库获取到文件?