我一直试图根据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()
答案 0 :(得分:0)
获得准确的结果,ix
和iy
是具有您想要的精确度的浮点数。问题是
print 'x = %d, y = %d' % (ix, iy)
%d
表示该数字应显示为整数,正好在这里发生。如果您试用%f
表示浮动表示:
print 'x = %f, y = %f' % (ix, iy)
您会发现自己获得了准确的结果。