[英]Matplotlib B1-Motion (mouse motion with key held down) equivalent?
是否存在類似於Tkinter <B1-Motion>
鍵的matplotlib事件? 這樣,僅當用戶在移動鼠標的同時按住B1時才調用函數嗎?
我目前正在研究本文檔,但沒有看到這樣的選擇,如果沒有,我可以通過一些簡單的方法來重新創建它嗎?
def notify_motion(event):
"""
Only called when button 1 is clicked and motion detected
"""
...
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()
我不知道您是否仍然在乎這個問題,無論如何,有一個使用matplotlib.backend_bases.MouseEvent
類的button
屬性的非常簡單的解決方案,因為如果按下光標, event.button
將為1
如果按下則為None
被釋放。
以下是我的代碼,通過按住並移動光標來畫一條線:
import matplotlib.pyplot as plt
import numpy as np
def moved_and_pressed(event):
if event.button==1:
x = np.append(line.get_xdata(), event.xdata)
y = np.append(line.get_ydata(), event.ydata)
line.set_data(x, y)
fig.canvas.draw()
fig, ax = plt.subplots(1,1, figsize=(5,3), dpi=100)
line, = ax.plot([], [], 'k')
ax.set_xlim(0,10); ax.set_ylim(0,10)
cid = fig.canvas.mpl_connect('motion_notify_event', moved_and_pressed)
plt.show()
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.