【发布时间】:2017-07-14 11:00:00
【问题描述】:
我一直在尝试根据 matplotlib 绘图比例而不是像素将鼠标 x,y 坐标转换为变量,但它只返回整数分量,如 0.0 或 1.0 我想返回准确的数字,例如 0.1245 这是我的代码
import matplotlib
import Tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt
def onclick(self,event):
ix, iy = float(event.xdata), float(event.ydata)
print 'x = %d, y = %d' % (
ix, iy)
root = tk.Tk()
circle1 = plt.Circle((0, 0), 1, color='blue')
f = plt.figure()
a = f.add_subplot(111)
f, a = plt.subplots()
a.add_artist(circle1)
a.set_xlim(-1.1, +1.1)
a.set_ylim(-1.1, +1.1)
#a.plot(circle1)
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.mpl_connect('button_press_event', onclick)
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
root.mainloop()
【问题讨论】:
-
这很有趣。我正在 Python 3 中尝试这个,除了细微的变化(
Tkinter->tkinter、字符串格式和def onclick(self, event)->def onclick(event)),它给了我非常准确的结果。 -
实际上是的,python 3 与 python 不同,有一些细微的变化,谢谢回复:)
标签: python matplotlib tkinter mouseevent coordinates