最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytho matplotlib工具欄源碼探析一之禁用工具欄、默認工具欄和工具欄管理器三種模式的差異

 更新時間:2021年02月25日 15:27:01   作者:mighty13  
這篇文章主要介紹了pytho matplotlib工具欄源碼探析一之禁用工具欄、默認工具欄和工具欄管理器三種模式的差異,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

使用matplotlib繪圖時,在彈出的窗口中默認是有工具欄的,那么這些工具欄是如何定義的呢?

工具欄的三種模式

matplotlib的基礎配置由運行時參數(shù)(rcParams)控制,導入matplotlib時,加載matplotlibrc文件生成默認運行時參數(shù)。
查看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

工具欄最終依靠后端實現(xiàn),不同的后端具體實現(xiàn)會有一些差異,我選擇的后端是Pyqt5,通過查看模塊matplotlib.backends.backend_qt5源碼可知,matplotlib在利用后端生成窗口時根據(jù)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后端默認模式工具欄的具體實現(xiàn)。
  • 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')]

根據(jù)源碼可知,列表中每個元組為工具項定義,元組的四個元素分別表示按鈕名稱、按鈕提示文本、按鈕圖像、按鈕對應方法。

# 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)類:工具欄容器的基類,定義了工具欄編輯的方法。構造函數(shù)參數(shù)為toolmanager,表示工具欄容器容納的工具欄。
  • matplotlib.backend_managers.ToolManager(figure=None)類:管理用戶觸發(fā)工具欄工具項按鈕而產(chǎn)生的動作。
  • 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函數(shù):設置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工具欄內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Django 用戶認證Auth組件的使用

    Django 用戶認證Auth組件的使用

    這篇文章主要介紹了Django 用戶認證Auth組件的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • python爬蟲之快速對js內容進行破解

    python爬蟲之快速對js內容進行破解

    這篇文章主要介紹了python爬蟲之快速對js內容進行破解,到一般js破解有兩種方法,一種是用Python重寫js邏輯,一種是利用第三方庫來調用js內容獲取結果,這次我們就用第三方庫來進行js破解,需要的朋友可以參考下
    2019-07-07
  • numpy中實現(xiàn)ndarray數(shù)組返回符合特定條件的索引方法

    numpy中實現(xiàn)ndarray數(shù)組返回符合特定條件的索引方法

    下面小編就為大家分享一篇numpy中實現(xiàn)ndarray數(shù)組返回符合特定條件的索引方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python實現(xiàn)生成隨機數(shù)據(jù)插入mysql數(shù)據(jù)庫的方法

    Python實現(xiàn)生成隨機數(shù)據(jù)插入mysql數(shù)據(jù)庫的方法

    這篇文章主要介紹了Python實現(xiàn)生成隨機數(shù)據(jù)插入mysql數(shù)據(jù)庫的方法,涉及Python隨機字符串生成及數(shù)據(jù)庫連接、插入等相關操作技巧,需要的朋友可以參考下
    2017-12-12
  • Python Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法匯總

    Python Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法匯總

    這篇文章主要介紹了Python中的Pandas創(chuàng)建Dataframe數(shù)據(jù)框的六種方法,創(chuàng)建Dataframe主要是使用pandas中的DataFrame函數(shù),其核心就是第一個參數(shù):data,傳入原始數(shù)據(jù),因此我們可以據(jù)此給出六種創(chuàng)建Dataframe的方法,需要的朋友可以參考下
    2023-05-05
  • Python映射拆分操作符用法實例

    Python映射拆分操作符用法實例

    這篇文章主要介紹了Python映射拆分操作符用法,實例分析了Python映射拆分操作符**的使用技巧,需要的朋友可以參考下
    2015-05-05
  • python實現(xiàn)pptx批量向PPT中插入圖片

    python實現(xiàn)pptx批量向PPT中插入圖片

    大家好,本篇文章主要講的是python實現(xiàn)pptx批量向PPT中插入圖片,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Python中關于print和return的區(qū)別

    Python中關于print和return的區(qū)別

    這篇文章主要介紹了Python中關于print和return的區(qū)別,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • numpy基礎教程之np.linalg

    numpy基礎教程之np.linalg

    這篇文章主要給大家介紹了關于numpy基礎教程之np.linalg的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • Python將腳本程序轉變?yōu)榭蓤?zhí)行程序的實現(xiàn)

    Python將腳本程序轉變?yōu)榭蓤?zhí)行程序的實現(xiàn)

    本文主要介紹了Python將腳本程序轉變?yōu)榭蓤?zhí)行程序的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02

最新評論

阳山县| 镇赉县| 若羌县| 河津市| 邵阳县| 全州县| 虎林市| 营口市| 双流县| 苏州市| 洪江市| 安龙县| 山西省| 慈溪市| 阳春市| 长寿区| 贞丰县| 杂多县| 崇仁县| 福海县| 鸡泽县| 平泉县| 兴山县| 汾西县| 肥西县| 吉隆县| 镇坪县| 丰原市| 澄城县| 罗平县| 镇平县| 长子县| 北川| 贺兰县| 黑水县| 康平县| 闽侯县| 永济市| 蓝田县| 府谷县| 京山县|