一个小型库,用于简单方便的matplotlib事件处理
mpl-events的Python项目详细描述
MPL事件
mpl events是一个小型库,用于简单方便的matplotlib事件处理 最小样板代码。换句话说,库提供了使用matplotlib event system的高级api。
如果要交互操作图形和绘图/可视化,则需要处理matplotlib事件。
matplotlib包含用于事件处理的低级api:使用FigureCanvasBase.mpl_connect
和
FigureCanvasBase.mpl_disconnect
方法、基于字符串的事件名和整数连接标识符。
利弊得失
pros:
- MPL事件提供高级API、自动断开连接和清除功能
- 不使用基于字符串的事件类型/名称。intstead,
MplEvent
枚举类用于所有事件类型。 - 不使用整数连接标识符。相反,事件和处理程序之间的连接是通过类
MplEventConnection
- MPL事件对象不拥有MPL图,也不创建对图或画布的其他引用
- mpl事件提供了方便的基类
MplEventDispatcher
,它包含处理程序api(带有类型提示),用于在一个类中处理所有mpl事件,而不需要样板代码
cons:
- 额外的抽象级别(如果这被认为是一个缺点的话)
- 项目中的其他依赖项
安装
您可以使用pip安装mpl事件:
pip install mpl-events
或Github回购:
pip install git+https://github.com/espdev/mpl-events.git
用法
事件调度员
可以创建自定义事件分派器类来处理一些matplotlib事件
继承MplEventDispatcher
类并实现所需的事件处理程序。
下面的示例演示如何创建用于处理所有鼠标事件的调度程序:
frommatplotlibimportpyplotaspltfrommpl_eventsimportMplEventDispatcher,mplclassMouseEventDispatcher(MplEventDispatcher):defon_mouse_button_press(self,event:mpl.MouseEvent):print(f'mouse button {event.button} pressed')defon_mouse_button_release(self,event:mpl.MouseEvent):print(f'mouse button {event.button} released')defon_mouse_move(self,event:mpl.MouseEvent):print(f'mouse moved')defon_mouse_wheel_scroll(self,event:mpl.MouseEvent):print(f'mouse wheel scroll {event.step}')figure=plt.figure()# setup figure and make plots is here ...mouse_dispatcher=MouseEventDispatcher(figure)plt.show()
MplEventDispatcher
类为所有matplotlib事件提供api(处理程序方法接口)。
您可以重写并实现其中一些方法来处理相应的事件。
调度程序可以使用mpl对象figure
或axes
(或canvas
)连接到画布。
总的来说,我们不需要考虑这个问题。我们通常只将figure
实例传递给构造函数。
默认情况下,会自动连接到事件。此行为由connect
参数控制。
就这样。我们不需要担心连接/断开连接或记住mpl事件名称。
如果我们想使用其他方法(而不是MplEventDispatcher
api)来处理事件,我们可以
在Dispatcher类中使用mpl_event_handler
decorator。
frommpl_eventsimportMplEventDispatcher,MplEvent,mpl_event_handler,mplclassCloseEventDispatcher(MplEventDispatcher):@mpl_event_handler(MplEvent.FIGURE_CLOSE)def_close_event_handler(self,event:mpl.CloseEvent):print(f'figure {event.canvas.figure} closing')
我们还可以创建事件调度器层次结构:
frommpl_eventsimportMplEventDispatcher,mplclassMyEventDispatcherBase(MplEventDispatcher):defon_figure_close(self,event:mpl.CloseEvent):print('figure closing from MyEventDispatcherBase')classMyEventDispatcher(MyEventDispatcherBase):defon_figure_close(self,event:mpl.CloseEvent):super().on_figure_close(event)print('figure closing from MyEventDispatcher')defon_figure_resize(self,event:mpl.ResizeEvent):print('figure resizing')
事件连接
事件和处理程序之间的连接在MplEventConnection
类中增加。
这个类是figure.canvas.mpl_connect
/figure.canvas.mpl_disconnect
mpl api的高级包装器。
MplEventConnection
如果我们想处理事件并且不使用事件分派器接口,则可以使用。
在本例中,我们只创建MplEventConnection
类的实例并传递给构造函数
用于连接(figure
、axes
或canvas
)的MPL对象,事件类型为MplEvent
枚举,处理程序为可调用。
默认情况下,将自动建立连接。此行为由connect
参数控制。
frommatplotlibimportpyplotaspltfrommpl_eventsimportMplEventConnection,MplEvent,mpldefclose_handler(event:mpl.CloseEvent):print('figure closing')figure=plt.figure()conn=MplEventConnection(figure,MplEvent.FIGURE_CLOSE,close_handler)print(conn)# MplEventConnection(event=<FIGURE_CLOSE:close_event>, handler=<function close_handler at 0x0000013FD1002E18>, id=5)plt.show()
我们还可以使用MplEvent
类的make_connection
方法来构造MplEventConnection
快捷方式:
frommpl_eventsimportMplEvent...conn=MplEvent.FIGURE_CLOSE.make_connection(figure,close_handler)
禁用默认按键事件处理程序
matplotlib图形通常包含一些与轴交互的导航栏,该导航栏处理按键。
默认情况下,按键处理程序连接在FigureManagerBase
mpl类中。
MPL事件提供disable_default_key_press_handler
函数来断开默认按键处理程序的连接。
同样在事件分派器类中,我们可以使用disable_default_handlers
属性。
下面是一个简单的示例:
frommatplotlibimportpyplotaspltfrommpl_eventsimportMplEventDispatcher,mplclassKeyEventDispatcher(MplEventDispatcher):disable_default_handlers=Truedefon_key_press(self,event:mpl.KeyEvent):print(f'Pressed key {event.key}')defon_key_release(self,event:mpl.KeyEvent):print(f'Released key {event.key}')figure=plt.figure()dispatcher=KeyEventDispatcher(figure)plt.show()
测试
我们用pytest和tox进行检测。