一个小型库,用于简单方便的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如何忽略缓冲读取器中在“”之后的行的其余部分,或行上的特定字符?   java在db中创建空对象或稍后保存   java如何实现UI无关的后台工作任务   java未能在Android中从BaseAdapter扩展的类中启动Tactivity?   java斐波那契迭代移动数组[]   安卓从文件读取提供了java。木卫一。StreamCorruptedException:无效的流标头:73720027   java计算矩形中的六边形数?   仅使用Java 1.5(或更早版本)读写XML   java如果所有元素都以相同的bucked结尾,为什么要进行大小调整?   java Apache POI Excel在xx中发现无法读取的内容。xlsx   swing我可以在普通java应用程序中使用GWTGUI吗?   来自自定义Java客户端的http删除请求的行为与邮递员不同   运行批处理文件时,java当前目录无效   使用TypeReference将java字符串转换为ArrayList<STRING>   documentlistener突出显示所有匹配词Java