使用Matplotlib在Python中使用鼠标在图像上绘制点
我正在尝试用鼠标在图像上绘制点。 问题是:图像出现了,但当我单击鼠标时,即使我多次单击,也不会打印任何内容 我的Python版本是Python2.7,带有Anaconda和IPython控制台。 在运行脚本之前,我在Ipython控制台中键入%pylab 这是我的密码:使用Matplotlib在Python中使用鼠标在图像上绘制点,python,matplotlib,plot,mouse,Python,Matplotlib,Plot,Mouse,我正在尝试用鼠标在图像上绘制点。 问题是:图像出现了,但当我单击鼠标时,即使我多次单击,也不会打印任何内容 我的Python版本是Python2.7,带有Anaconda和IPython控制台。 在运行脚本之前,我在Ipython控制台中键入%pylab 这是我的密码: import numpy as np from matplotlib import pyplot as plt #Some code here [. . .] fig, ax = plt.subplots(1) ax.ims
import numpy as np
from matplotlib import pyplot as plt
#Some code here [. . .]
fig, ax = plt.subplots(1)
ax.imshow(img, interpolation = 'bicubic')
'''preventing plot from rescaling image:'''
ax.set_xlim([0.0, img.shape[1]])
ax.set_ylim([img.shape[0], 0.0])
ax.hold(True)
ax.autoscale = False
#ax.plot(100,100, 'ro') # This works
class MouseMonitor:
flag = True
x = 0.
y = 0.
fig = None
axes = None
def __init__(self, fig, ax):
self.axes = ax
self.fig = fig
def __call__(self, event):
if self.flag:
print('({}, {})'.format(event.xdata, event.ydata))
self.flag = False
else:
d = np.linalg.norm([event.xdata - self.x, event.ydata - self.y])
print('({}, {})\n\n distance between points: {} m\n\n-------------------\n'.format(event.xdata, event.ydata, d))
self.flag = True
self.x = event.xdata
self.y = event.ydata
self.axes.plot(self.y, self.x, 'ro', linewidth = 5) #This don't work
mouse = MouseMonitor(fig, ax)
cid = fig.canvas.mpl_connect('button_press_event', mouse)
来自tcaswell评论的答复:
将self.axes.figure.canvas.draw_空闲调用添加到回调中。您需要将self.axes.figure.canvas.draw_空闲调用添加到回调中,直到画布重新绘制。OO方法懒得避免不必要的重画。self.fig.canvas.draw_idle也可以工作,没有看到您同时捕获了这两种方法。非常感谢。你应该用这些信息回答你自己的问题。