Matplotlib B1-Motion(按住键的鼠标运动)相当于?

时间:2015-07-06 14:17:15

标签: python python-2.7 matplotlib

是否存在类似于Tkinter <B1-Motion>键的matplotlib事件?这样只有当用户在移动鼠标的同时按住B1时才调用函数?

我目前正在查看this文档,但没有看到这样的选项,如果没有,有什么简单的方法可以重新创建吗?

def notify_motion(event):
    """
    Only called when button 1 is clicked and motion detected
    """

    ...

2 个答案:

答案 0 :(得分:2)

canvas.mpl_connect未提供key_down +动作事件。可能的黑客是存储鼠标按钮(或键)是否按下并使用motion_notify_event的属性。

在这个示例中,在wx中,我们可以按住鼠标并在绘图上绘制一个红色矩形:

import wx
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.patches import Rectangle
import matplotlib.cm as cm
import matplotlib.pyplot as plt

class Frame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent=parent)
        self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)
        self.SetSizer(self.boxSizer1)
        self.figure = Figure()
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.boxSizer1.Add(self.canvas, 1, wx.EXPAND)
        self.x0 = None
        self.y0 = None
        self.x1 = None
        self.y1 = None
        self.axes   = [self.figure.add_subplot(111), ]
        self.axes[0].plot(range(100), range(100))
        self.canvas.Bind(wx.EVT_ENTER_WINDOW, self.ChangeCursor)
        self.canvas.mpl_connect('button_press_event', self.on_press)
        self.canvas.mpl_connect('button_release_event', self.on_release)
        self.canvas.mpl_connect('motion_notify_event', self.on_motion)
        self.rect = Rectangle((0,0), 1, 1, fill=False, ec='r')
        self.axes[0].add_patch(self.rect)
        #store a property of whether the mouse key is pressed
        self.pressed = False



    def ChangeCursor(self, event):
        '''Change cursor into crosshair type when enter the plot area'''
        self.canvas.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))



    def on_press(self, event):
        '''Draw ROI rectangle'''
        self.pressed = True
        self.x0 = int(event.xdata)
        self.y0 = int(event.ydata)


    def on_release(self, event):
        '''When mouse is on plot and button is released, redraw ROI rectangle, update ROI values'''
        self.pressed = False
        self.redraw_rect(event)


    def redraw_rect(self, event):
        '''Draw the ROI rectangle overlay'''
        try:
            self.x1 = int(event.xdata)
            self.y1 = int(event.ydata)
            self.rect.set_xy((self.x0, self.y0))
            self.rect.set_width(self.x1 - self.x0)
            self.rect.set_height(self.y1 - self.y0)
        except:
            pass
        self.canvas.draw()

    def on_motion(self, event):
        '''If the mouse is on plot and if the mouse button is pressed, redraw ROI rectangle'''
        if self.pressed:
            # redraw the rect
            self.redraw_rect(event)

class App(wx.App):
    def OnInit(self):
        frame = Frame(None)
        self.SetTopWindow(frame)
        frame.Show(True)
        return True

if __name__=='__main__':
    app=App(0)
    app.MainLoop()

enter image description here

答案 1 :(得分:1)

我不知道你是否仍然关心这个问题,无论如何,使用matplotlib.backend_bases.MouseEvent类的button属性有一个非常简单的解决方案,因为event.button将{{1}如果正在按下光标,则1如果正在被释放。

以下是我通过按住并移动光标来绘制线条的代码:

None

希望它有所帮助。