使用鼠标单击获取matplotlib图图形python的坐标 [英] get Coordinates of matplotlib plot figure python with mouse click

查看:50
本文介绍了使用鼠标单击获取matplotlib图图形python的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试根据matplotlib绘图比例而不是像素将鼠标的x,y坐标设置为变量,但它仅返回整数分量,例如0.0或1.0我想返回准确的数字,例如0.1245这是我的代码

I have been trying to get the mouse x,y coordinates to variables according to matplotlib plot scale not pixels but it only returns me the integer components like 0.0 or 1.0 I want to return the accurate number like 0.1245 here is my code

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()

推荐答案

会获得准确的结果, ix iy 包含以下内容:您想要的准确性.问题是格式为

You are getting accurate results, ix and iy are floats with the accuracy you want. The problem is the formatting in

print 'x = %d, y = %d' % (ix, iy)

%d 表示该数字应显示为整数,而这恰好在此处发生.如果您尝试使用%f 进行浮点表示:

%d means that the number should be displayed as an integer, and exactly that is happening here. If you try out %f for float representation:

print 'x = %f, y = %f' % (ix, iy)

您会看到得到的是准确的结果.

you'll see that you're getting accurate results.

这篇关于使用鼠标单击获取matplotlib图图形python的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆