python鼠标画点连线_使用Matplotlib在Python中使用鼠标在图像上绘制点

我试图用鼠标在图像上绘制点。

问题是:图像出现了,但是当我点击鼠标时,什么也没有绘制出来(即使我点击了几次)。在

我的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.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)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值