python.matplotlib实现手动鼠标移动坐标点

系统 1593 0

01 操作动画

  • 随意拖动任一点,任意运动
    python.matplotlib实现手动鼠标移动坐标点_第1张图片

02 关键代码

  • 编程:python 3.6
  • 运行环境:Pycharm
  • 只展示部分关键代码,需要源码的见文末链接
  • 关键:一定要注意能不能运动,由于坐标轴的设置区间不同,offset的偏差值,一定要对应好。否则,鼠标无法识别图中的点,造成无法移动的假象。
            
              '''
设置:单点的动画移动
'''
    def __init__(self):
        # 创建figure(绘制面板)、创建图表(axes)
        self.fig,self.ax = plt.subplots()
        # 设置标题
        self.ax.set_title('Click and drag a point to move it')
        # 设置坐标轴范围
        self.ax.set_xlim((-2, 4))
        self.ax.set_ylim((-2, 4))
        # 设置初始值
        self.x = [1,2]
        self.y = [1,2]
        # 绘制2D的动画line
        self.line = Line2D(self.x, self.y, ls="",
                           marker='o', markerfacecolor='r',
                           animated=True)
        self.ax.add_line(self.line)
        # 标志值设为none
        self._ind = None
        # 设置画布,方便后续画布响应事件
        canvas = self.fig.canvas
        canvas.mpl_connect('draw_event', self.draw_callback)
        canvas.mpl_connect('button_press_event', self.button_press_callback)
        canvas.mpl_connect('button_release_event', self.button_release_callback)
        canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
        self.canvas = canvas
        plt.grid()
        plt.show()


    def get_ind_under_point(self, event):
        'get the index of the vertex under point if within epsilon tolerance'
        # 在公差允许的范围内,求出鼠标点下顶点坐标的数值
        xt,yt = np.array(self.x),np.array(self.y)
        d = np.sqrt((xt-event.xdata)**2 + (yt-event.ydata)**2)
        indseq = np.nonzero(np.equal(d, np.amin(d)))[0]
        ind = indseq[0]
        # 如果在公差范围内,则返回ind的值
        if d[ind] >=self.offset:
            ind = None
        return ind

    # 鼠标移动的事件
    def motion_notify_callback(self, event):
        'on mouse movement'
        if not self.showverts: return
        if self._ind is None: return
        if event.inaxes is None: return
        if event.button != 1: return
        # 更新数据
        x,y = event.xdata, event.ydata
        self.x[self._ind] = x
        self.y[self._ind] = y
        # 根据更新的数值,重新绘制图形
        self.line = Line2D(self.x, self.y, ls="",
                           marker='o', markerfacecolor='r',
                           animated=True)
        self.ax.add_line(self.line)
        # 恢复背景
        self.canvas.restore_region(self.background)
        self.ax.draw_artist(self.line)
        self.canvas.blit(self.ax.bbox)


            
          

03 源码分享

  • 源码已经上传到我的上传资料
            
              https://download.csdn.net/download/kyrie001/11227186

            
          

辛苦研究两天的成果,如果帮到你,希望点个赞。


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论