濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > pytho matplotlib工具欄源碼探析一之禁用工具欄、默認(rèn)工具欄和工具欄管理器三種模式的差異

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

熱門(mén)標(biāo)簽:正安縣地圖標(biāo)注app 螳螂科技外呼系統(tǒng)怎么用 電銷機(jī)器人系統(tǒng)廠家鄭州 舉辦過(guò)冬奧會(huì)的城市地圖標(biāo)注 遼寧智能外呼系統(tǒng)需要多少錢(qián) 400電話申請(qǐng)資格 qt百度地圖標(biāo)注 阿里電話機(jī)器人對(duì)話 地圖地圖標(biāo)注有嘆號(hào)

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

工具欄的三種模式

matplotlib的基礎(chǔ)配置由運(yùn)行時(shí)參數(shù)(rcParams)控制,導(dǎo)入matplotlib時(shí),加載matplotlibrc文件生成默認(rèn)運(yùn)行時(shí)參數(shù)。
查看matplotlibrc文件可知#toolbar: toolbar2 # {None, toolbar2, toolmanager},即工具欄有三種模式None、toolbar2toolmanager,其中默認(rèn)模式為toolbar2。

工具欄模式切換

通過(guò)類似語(yǔ)句plt.rcParams['toolbar'] = 'None'可控制工具欄的模式。
需要注意的是plt.rcParams['toolbar'] = 'None'應(yīng)當(dāng)放置在圖像實(shí)例化之前。

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

toolbar2模式:默認(rèn)工具欄布局。
plt.rcParams['toolbar'] = 'toolbar2'

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

工具欄模式切換原理

和工具欄相關(guān)的模塊有:

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

工具欄最終依靠后端實(shí)現(xiàn),不同的后端具體實(shí)現(xiàn)會(huì)有一些差異,我選擇的后端是Pyqt5,通過(guò)查看模塊matplotlib.backends.backend_qt5源碼可知,matplotlib在利用后端生成窗口時(shí)根據(jù)rcParams['toolbar']的值選擇不同的工具欄構(gòu)造方式。

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

默認(rèn)模式(toolbar2)原理

與該模式相關(guān)的重要定義有:

  • matplotlib.backend_bases.NavigationToolbar2(canvas)類:默認(rèn)的toolbar2模式工具欄的基類,后端需要通過(guò)canvas對(duì)象處理工具欄按鈕事件、覆蓋構(gòu)造方法初始化工具欄、覆蓋save_figure()等方法。
  • matplotlib.backends.backend_qt5.NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar)類:定義了QT后端默認(rèn)模式工具欄的具體實(shí)現(xiàn)。
  • matplotlib.backend_bases.FigureCanvasBase類:canvas對(duì)象的基類,通過(guò)toolbar屬性與工具欄進(jìn)行連接。
  • matplotlib.backend_bases.NavigationToolbar2(canvas).toolitems屬性:定義了默認(rèn)模式工具欄工具項(xiàng)列表。

案例:驗(yàn)證默認(rèn)模式工具欄布局

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ù)源碼可知,列表中每個(gè)元組為工具項(xiàng)定義,元組的四個(gè)元素分別表示按鈕名稱、按鈕提示文本、按鈕圖像、按鈕對(duì)應(yīng)方法。

# 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)原理

與該模式相關(guān)的重要定義有:

  • matplotlib.backend_bases.ToolContainerBase(toolmanager)類:工具欄容器的基類,定義了工具欄編輯的方法。構(gòu)造函數(shù)參數(shù)為toolmanager,表示工具欄容器容納的工具欄。
  • matplotlib.backend_managers.ToolManager(figure=None)類:管理用戶觸發(fā)工具欄工具項(xiàng)按鈕而產(chǎn)生的動(dòng)作。
  • matplotlib.backend_tools.ToolBase類:所有工具欄工具項(xiàng)的基類,所有工具項(xiàng)均由matplotlib.backend_managers.ToolManager實(shí)例化。
  • matplotlib.backend_tools.default_tools變量:字典類型,實(shí)例化基于matplotlib.backend_tools.ToolBase類定義的內(nèi)置工具項(xiàng)。
  • matplotlib.backend_tools.default_toolbar_tools變量:嵌套列表,以類似格式[[分組1, [工具1, 工具2 ...]], [分組2, [...]]]定義工具欄布局。
  • matplotlib.backend_tools.add_tools_to_container函數(shù):設(shè)置toolbarmanager模式默認(rèn)工具欄。

案例:驗(yàn)證工具欄管理器模式工具欄布局

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>)]}

到此這篇關(guān)于pytho matplotlib工具欄源碼探析一之禁用工具欄、默認(rèn)工具欄和工具欄管理器三種模式的差異的文章就介紹到這了,更多相關(guān)pytho matplotlib工具欄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python matplotlib工具欄源碼探析二之添加、刪除內(nèi)置工具項(xiàng)的案例
  • python matplotlib工具欄源碼探析三之添加、刪除自定義工具項(xiàng)的案例詳解
  • 詳解python安裝matplotlib庫(kù)三種失敗情況
  • Python matplotlib讀取excel數(shù)據(jù)并用for循環(huán)畫(huà)多個(gè)子圖subplot操作

標(biāo)簽:濟(jì)源 興安盟 信陽(yáng) 昭通 淘寶好評(píng)回訪 隨州 合肥 阜新

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytho matplotlib工具欄源碼探析一之禁用工具欄、默認(rèn)工具欄和工具欄管理器三種模式的差異》,本文關(guān)鍵詞  pytho,matplotlib,工具欄,源碼,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《pytho matplotlib工具欄源碼探析一之禁用工具欄、默認(rèn)工具欄和工具欄管理器三種模式的差異》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于pytho matplotlib工具欄源碼探析一之禁用工具欄、默認(rèn)工具欄和工具欄管理器三種模式的差異的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    乌苏市| 赫章县| 招远市| 右玉县| 汽车| 镇雄县| 沂南县| 普兰店市| 兰西县| 涞源县| 兴仁县| 徐水县| 南丹县| 界首市| 江永县| 宁海县| 若尔盖县| 永修县| 普格县| 顺平县| 鸡泽县| 蓝田县| 庆元县| 鄢陵县| 曲阜市| 湘乡市| 营口市| 昆山市| 烟台市| 剑川县| 铜陵市| 济阳县| 营口市| 额济纳旗| 新闻| 曲靖市| 台北县| 博野县| 多伦县| 炎陵县| 重庆市|