pytho matplotlib工具栏源码探析一之禁用工具栏、默认工具栏和工具栏管理器三种模式的差异

使用matplotlib绘图时,在弹出的窗口中默认是有工具栏的,那么这些工具栏是如何定义的呢?

工具栏的三种模式

matplotlib的基础配置由运行时参数(rcParams)控制,导入matplotlib时,加载matplotlibrc文件生成默认运行时参数。
查看matplotlibrc文件可知#toolbar: toolbar2 # {None, toolbar2, toolmanager},即工具栏有三种模式Nonetoolbar2toolmanager,其中默认模式为toolbar2

工具栏模式切换

通过类似语句plt.rcParams['toolbar'] = 'None'可控制工具栏的模式。
需要注意的是plt.rcParams['toolbar'] = 'None'应当放置在图像实例化之前。

None模式:禁用工具栏。
plt.rcParams['toolbar'] = 'None'

toolbar2模式:默认工具栏布局。
plt.rcParams['toolbar'] = 'toolbar2'

toolmanager模式:工具栏布局模式与toolbar2模式稍有不同。
plt.rcParams['toolbar'] = 'toolmanager'

工具栏模式切换原理

和工具栏相关的模块有:

  • matplotlib.backend_bases
  • matplotlib.backend_managers
  • matplotlib.backend_tools
  • matplotlib.backends

工具栏最终依靠后端实现,不同的后端具体实现会有一些差异,我选择的后端是Pyqt5,通过查看模块matplotlib.backends.backend_qt5源码可知,matplotlib在利用后端生成窗口时根据rcParams['toolbar']的值选择不同的工具栏构造方式。

def _get_toolbar(self, canvas, parent):
  # must be inited after the window, drawingArea and figure
  # attrs are set
  if matplotlib.rcParams['toolbar'] == 'toolbar2':
    toolbar = NavigationToolbar2QT(canvas, parent, True)
  elif matplotlib.rcParams['toolbar'] == 'toolmanager':
    toolbar = ToolbarQt(self.toolmanager, self.window)
  else:
    toolbar = None
  return toolbar

默认模式(toolbar2)原理

与该模式相关的重要定义有:

  • matplotlib.backend_bases.NavigationToolbar2(canvas)类:默认的toolbar2模式工具栏的基类,后端需要通过canvas对象处理工具栏按钮事件、覆盖构造方法初始化工具栏、覆盖save_figure()等方法。
  • matplotlib.backends.backend_qt5.NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar)类:定义了QT后端默认模式工具栏的具体实现。
  • matplotlib.backend_bases.FigureCanvasBase类:canvas对象的基类,通过toolbar属性与工具栏进行连接。
  • matplotlib.backend_bases.NavigationToolbar2(canvas).toolitems属性:定义了默认模式工具栏工具项列表。

案例:验证默认模式工具栏布局

import matplotlib.pyplot as plt

fig=plt.gcf()
toolbar = fig.canvas.manager.toolbar
print(toolbar.toolitems)

输出:

[('Home', 'Reset original view', 'home', 'home'),
 ('Back', 'Back to previous view', 'back', 'back'),
 ('Forward', 'Forward to next view', 'forward', 'forward'),
 (None, None, None, None),
 ('Pan', 'Left button pans, Right button zooms\nx/y fixes axis, CTRL fixes aspect', 'move', 'pan'),
 ('Zoom', 'Zoom to rectangle\nx/y fixes axis, CTRL fixes aspect', 'zoom_to_rect', 'zoom'),
 ('Subplots', 'Configure subplots', 'subplots', 'configure_subplots'),
 ('Customize', 'Edit axis, curve and image parameters', 'qt4_editor_options', 'edit_parameters'),
 (None, None, None, None),
 ('Save', 'Save the figure', 'filesave', 'save_figure')]

根据源码可知,列表中每个元组为工具项定义,元组的四个元素分别表示按钮名称、按钮提示文本、按钮图像、按钮对应方法。

# list of toolitems to add to the toolbar, format is:
# (
#  text, # the text of the button (often not visible to users)
#  tooltip_text, # the tooltip shown on hover (where possible)
#  image_file, # name of the image for the button (without the extension)
#  name_of_method, # name of the method in NavigationToolbar2 to call
# )

工具栏管理器模式(toolmanager)原理

与该模式相关的重要定义有:

  • matplotlib.backend_bases.ToolContainerBase(toolmanager)类:工具栏容器的基类,定义了工具栏编辑的方法。构造函数参数为toolmanager,表示工具栏容器容纳的工具栏。
  • matplotlib.backend_managers.ToolManager(figure=None)类:管理用户触发工具栏工具项按钮而产生的动作。
  • matplotlib.backend_tools.ToolBase类:所有工具栏工具项的基类,所有工具项均由matplotlib.backend_managers.ToolManager实例化。
  • matplotlib.backend_tools.default_tools变量:字典类型,实例化基于matplotlib.backend_tools.ToolBase类定义的内置工具项。
  • matplotlib.backend_tools.default_toolbar_tools变量:嵌套列表,以类似格式[[分组1, [工具1, 工具2 ...]], [分组2, [...]]]定义工具栏布局。
  • matplotlib.backend_tools.add_tools_to_container函数:设置toolbarmanager模式默认工具栏。

案例:验证工具栏管理器模式工具栏布局

import matplotlib.pyplot as plt

plt.rcParams['toolbar'] = 'toolmanager'
fig=plt.gcf()
toolbar= fig.canvas.manager.toolbar
print(toolbar._toolitems)

输出:

{'home': [(<PyQt5.QtWidgets.QToolButton object at 0x00000289EABBC1F8>, <function ToolbarQt.add_toolitem.<locals>.handler at 0x00000289EB0BC510>)],
 'back': [(<PyQt5.QtWidgets.QToolButton object at 0x00000289EAE86678>, <function ToolbarQt.add_toolitem.<locals>.handler at 0x00000289EB0BC598>)],
 'forward': [(<PyQt5.QtWidgets.QToolButton object at 0x00000289EAE8B4C8>, <function ToolbarQt.add_toolitem.<locals>.handler at 0x00000289EB0BC620>)],
 'pan': [(<PyQt5.QtWidgets.QToolButton object at 0x00000289EAE8BAF8>, <function ToolbarQt.add_toolitem.<locals>.handler at 0x00000289EB0BC6A8>)],
 'zoom': [(<PyQt5.QtWidgets.QToolButton object at 0x00000289EAE93DC8>, <function ToolbarQt.add_toolitem.<locals>.handler at 0x00000289EB0BC7B8>)],
 'subplots': [(<PyQt5.QtWidgets.QToolButton object at 0x00000289EAE93438>, <function ToolbarQt.add_toolitem.<locals>.handler at 0x00000289EB0BC8C8>)],
 'save': [(<PyQt5.QtWidgets.QToolButton object at 0x00000289EAE93678>, <function ToolbarQt.add_toolitem.<locals>.handler at 0x00000289EB0BC950>)],
 'help': [(<PyQt5.QtWidgets.QToolButton object at 0x00000289EAE93A68>, <function ToolbarQt.add_toolitem.<locals>.handler at 0x00000289EB0BC9D8>)]}

到此这篇关于pytho matplotlib工具栏源码探析一之禁用工具栏、默认工具栏和工具栏管理器三种模式的差异的文章就介绍到这了,更多相关pytho matplotlib工具栏内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Python matplotlib读取excel数据并用for循环画多个子图subplot操作

    读取excel数据需要用到xlrd模块,在命令行运行下面命令进行安装 pip install xlrd 表格内容大致如下,有若干sheet,每个sheet记录了同一所学校的所有学生成绩,分为语文.数学.英语.综合.总分 考号 姓名 班级 学校 语文 数学 英语 综合 总分 ... ... ... ... 136 136 100 57 429 ... ... ... ... 128 106 70 54 358 ... ... ... ... 110.5 62 92 44 308.5 画多张子图需要

  • 详解python安装matplotlib库三种失败情况

    (可能只有最后一句命令有用,可能全篇都没用) (小白方法,可能只适用于本人情况) 安装matplotlib时,出现的三种失败情况 1.read timed out 一开始我在pycharm终端使用pip install matplotlib时,出现的是下图所示情况,大致情况是安装时间太长,所以当时我用了清华镜像,将原来的命令改成了pip install -i https://mirrors.ustc.edu.cn/pypi/web/simple/ matplotlib,速度是上来了,但是还是安装

  • python matplotlib工具栏源码探析三之添加、删除自定义工具项的案例详解

    matplotlib工具栏源码探析二(添加.删除内置工具项)探讨了工具栏内置工具项的管理,除了内置工具项,很多场景中需要自定义工具项,官方给出了案例https://matplotlib.org/gallery/user_interfaces/toolmanager_sgskip.html,主要基于matplotlib.backend_managers.ToolManager类实现,即使用工具栏管理器模式. 官方案例解析 下面对官方案例关键点做注释说明. import matplotlib.pyp

  • python matplotlib工具栏源码探析二之添加、删除内置工具项的案例

    从matplotlib工具栏源码探析一(禁用工具栏.默认工具栏和工具栏管理器三种模式的差异)一文可知matplotlib内置实现了多个工具项的实现,而默认工具栏中的工具项只是其中的一部分,有没有方法直接管理工具栏,添加.删除内置工具项? matplotlib内置的工具项 由源码可知,matplotlib.backend_tools.default_tools变量为字典类型,实例化了基于matplotlib.backend_tools.ToolBase类定义的内置工具项. 源码 default_t

  • pytho matplotlib工具栏源码探析一之禁用工具栏、默认工具栏和工具栏管理器三种模式的差异

    使用matplotlib绘图时,在弹出的窗口中默认是有工具栏的,那么这些工具栏是如何定义的呢? 工具栏的三种模式 matplotlib的基础配置由运行时参数(rcParams)控制,导入matplotlib时,加载matplotlibrc文件生成默认运行时参数. 查看matplotlibrc文件可知#toolbar: toolbar2 # {None, toolbar2, toolmanager},即工具栏有三种模式None.toolbar2和toolmanager,其中默认模式为toolbar

  • 关于Redis网络模型的源码详析

    前言 Redis的网络模型是基于I/O多路复用程序来实现的.源码中包含四种多路复用函数库epoll.select.evport.kqueue.在程序编译时会根据系统自动选择这四种库其中之一.下面以epoll为例,来分析Redis的I/O模块的源码. epoll系统调用方法 Redis网络事件处理模块的代码都是围绕epoll那三个系统方法来写的.先把这三个方法弄清楚,后面就不难了. epfd = epoll_create(1024); 创建epoll实例 参数:表示该 epoll 实例最多可监听的

  • Java源码刨析之ArrayQueue

    目录 ArrayQueue内部实现 ArrayQueue源码剖析 总结 ArrayQueue内部实现 在谈ArrayQueue的内部实现之前我们先来看一个ArrayQueue的使用例子: public void testQueue() { ArrayQueue<Integer> queue = new ArrayQueue<>(10); queue.add(1); queue.add(2); queue.add(3); queue.add(4); System.out.printl

  • YOLOv5中SPP/SPPF结构源码详析(内含注释分析)

    目录 一.SPP的应用的背景 二.SPP结构分析 三.SPPF结构分析 四.YOLOv5中SPP/SPPF结构源码解析(内含注释分析) 总结 一.SPP的应用的背景 在卷积神经网络中我们经常看到固定输入的设计,但是如果我们输入的不能是固定尺寸的该怎么办呢? 通常来说,我们有以下几种方法: (1)对输入进行resize操作,让他们统统变成你设计的层的输入规格那样.但是这样过于暴力直接,可能会丢失很多信息或者多出很多不该有的信息(图片变形等),影响最终的结果. (2)替换网络中的全连接层,对最后的卷

  • Java源码刨析之ArrayDeque

    目录 前言 双端队列整体分析 数组实现ArrayDeque(双端队列)的原理 底层数据遍历顺序和逻辑顺序 ArrayDeque类关键字段分析 ArrayDeque构造函数分析 ArrayDeque关键函数分析 addLast函数分析 addFirst函数分析 doubleCapacity函数分析 pollLast和pollFirst函数分析 总结 前言 在本篇文章当中主要跟大家介绍JDK给我们提供的一种用数组实现的双端队列,在之前的文章LinkedList源码剖析当中我们已经介绍了一种双端队列,

  • Vue收集依赖与触发依赖源码刨析

    目录 定义依赖 收集依赖 触发依赖 总结 定义依赖 定义依赖是什么时候开始的呢?通过源码可以发现在执行_init函数的时候会执行initState(vm)方法: function initState(vm) { ... if (opts.data) { initData(vm); } else { var ob = observe((vm._data = {})); ob && ob.vmCount++; } ... } 先触发initData方法: function initData(v

  • SpringBoot拦截器以及源码详析

    目录 1.拦截器是什么 2.自定义拦截器 2.1 编写拦截器 2.2 注册和配置拦截器 3.拦截器原理 3.1 找到可以处理请求的handler以及handler的所有拦截器 3.2 执行拦截器的preHandle方法 3.3 执行目标方法 3.4 执行拦截器的postHandle方法 3.5 执行拦截器的afterCompletion方法 3.6 异常处理 4.总结 1.拦截器是什么 java里的拦截器(Interceptor)是动态拦截Action调用的对象,它提供了一种机制可以使开发者在一

  • Java1.8中StringJoiner的使用及源码详析

    前言 StringJoiner是Java里1.8新增的类,主要是帮助我们把一个列表拼接字符串, 或许有一部分人没有接触过. 所以本文将从使用例子入手, 分析StringJoiner的源码. 基本好的同学, 其实只要把这段例子自己运行一下, 自己看看源码就可以了.因为我觉得这个类挺简单的. 没必要看我下面的废话.... public class StringJoinerTest { public static void main(String[] args) { StringJoiner join

随机推荐