2
我一直在試圖讓鼠標x,y座標變量根據matplotlib繪圖比例不是像素,但它只返回我的整數組件像0.0或1.0 我想返回準確的數字,如0.1245 這裏是我的代碼獲取座標matplotlib情節圖python與鼠標點擊
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)'),它給了我完全準確的結果。 – TidB
actualy是python 3與python有一些小的變化不同,感謝回覆:) –