2015-07-06 5 views
1

Есть ли событие matplotlib, похожее на ключ Tkinter <B1-Motion>? Таким образом, функция вызывается только тогда, когда пользователь удерживает B1 при перемещении мыши?Matplotlib B1-Motion (движение мыши с нажатой клавишей) эквивалентно?

Я сейчас смотрю в this документацию, но не вижу такой опции, если нет, то какой-то простой способ я могу воссоздать это?

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

    ... 

ответ

2

canvas.mpl_connect не предоставляет событие key_down + motion. Возможным взломом является сохранение свойства того, удерживается ли кнопка мыши (или клавиша) и используется 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

Смежные вопросы