matplotlib 显示图片鼠标所指位置的坐标 |
您所在的位置:网站首页 › 鼠标垫的照片怎么保存 › matplotlib 显示图片鼠标所指位置的坐标 |
用PIL的Image打开我的图片: from PIL import Image img = Image.open('0.jpg')用matplotlib给出一个plot对象: import matplotlib.pyplot as plt fig = plt.figure()然后用到的是matplotlib 的一个函数 canvas.mpl_connect('button_press_event', on_press)(这里只有两个我关键用到的参数) 第一个参数表示鼠标点击的动作,函数会识别这个字符串,还有很多别的串代表别的一些功能,这里我需要的就是这个。 第二个参数是一个自定义函数,给出你鼠标点击时候所进行的操作,我就让鼠标点击的时候,打印出当前位置的坐标。 def on_press(event): print("you pressed" ,event.button, event.xdata, event.ydata)这里的event对象有几个属性: 1、button代表鼠标点击 2、xdata、ydata分别代表当前鼠标所在位置的横纵坐标 完整代码 import numpy as np import matplotlib.animation as animation import matplotlib.pyplot as plt from PIL import Image def on_press(event): print("my position:" ,event.button,event.xdata, event.ydata) fig = plt.figure() img = Image.open('0.jpg') #updata = True plt.imshow(img, animated= True) fig.canvas.mpl_connect('button_press_event', on_press) plt.show()(“参考matplotlib文档所总结”) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |