一个小型库,用于简单方便的matplotlib事件处理

mpl-events的Python项目详细描述


MPL事件

PyPI versionBuild statusDocs statusLicense

mpl events是一个小型库,用于简单方便的matplotlib事件处理 最小样板代码。换句话说,库提供了使用matplotlib event system的高级api。

如果要交互操作图形和绘图/可视化,则需要处理matplotlib事件。 matplotlib包含用于事件处理的低级api:使用FigureCanvasBase.mpl_connectFigureCanvasBase.mpl_disconnect方法、基于字符串的事件名和整数连接标识符。

利弊得失

pros

  • MPL事件提供高级API、自动断开连接和清除功能
  • 不使用基于字符串的事件类型/名称。intstead,MplEvent枚举类用于所有事件类型。
  • 不使用整数连接标识符。相反,事件和处理程序之间的连接是通过类MplEventConnection
  • MPL事件对象不拥有MPL图,也不创建对图或画布的其他引用
  • mpl事件提供了方便的基类MplEventDispatcher,它包含处理程序api(带有类型提示),用于在一个类中处理所有mpl事件,而不需要样板代码

cons

  • 额外的抽象级别(如果这被认为是一个缺点的话)
  • 项目中的其他依赖项

安装

Supported Python versions

您可以使用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对象figureaxes(或canvas)连接到画布。 总的来说,我们不需要考虑这个问题。我们通常只将figure实例传递给构造函数。 默认情况下,会自动连接到事件。此行为由connect参数控制。

就这样。我们不需要担心连接/断开连接或记住mpl事件名称。

如果我们想使用其他方法(而不是MplEventDispatcherapi)来处理事件,我们可以 在Dispatcher类中使用mpl_event_handlerdecorator。

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_disconnectmpl api的高级包装器。

MplEventConnection如果我们想处理事件并且不使用事件分派器接口,则可以使用。

在本例中,我们只创建MplEventConnection类的实例并传递给构造函数 用于连接(figureaxescanvas)的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图形通常包含一些与轴交互的导航栏,该导航栏处理按键。 默认情况下,按键处理程序连接在FigureManagerBasempl类中。 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进行检测。

文档

请看the latest documentation

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Admob不使用webview   Java Wicket表单:序列化对象类“myClassName”时出错   java googledriveapi更新文件与我的帐户管理员   java简单框架未知xml标记解析   java如何使用主类和用户获取Spark应用程序ID   java如何更改自定义属性的值?   java有没有办法在jMock中找到未使用的期望值?   gradle运行任务的java文档?   java通过使用数组来存储文本文件的行,通过覆盖来删除文本文件中的行   用于表达式语言注入的java利用负载   java IDEStyle程序运行   java在运行时启用/禁用Springws   爪哇芦苇。formatNumber NullPointException   java为什么我的代码无法检测两个动态实体之间的碰撞?