pytho matplotlib工具欄源碼探析一之禁用工具欄、默認(rèn)工具欄和工具欄管理器三種模式的差異
使用matplotlib
繪圖時,在彈出的窗口中默認(rèn)是有工具欄的,那么這些工具欄是如何定義的呢?
工具欄的三種模式
matplotlib
的基礎(chǔ)配置由運行時參數(shù)(rcParams
)控制,導(dǎo)入matplotlib
時,加載matplotlibrc
文件生成默認(rèn)運行時參數(shù)。
查看matplotlibrc
文件可知#toolbar: toolbar2 # {None, toolbar2, toolmanager}
,即工具欄有三種模式None
、toolbar2
和toolmanager
,其中默認(rèn)模式為toolbar2
。
工具欄模式切換
通過類似語句plt.rcParams['toolbar'] = 'None'
可控制工具欄的模式。
需要注意的是plt.rcParams['toolbar'] = 'None'
應(yīng)當(dāng)放置在圖像實例化之前。
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
工具欄最終依靠后端實現(xiàn),不同的后端具體實現(xiàn)會有一些差異,我選擇的后端是Pyqt5
,通過查看模塊matplotlib.backends.backend_qt5
源碼可知,matplotlib
在利用后端生成窗口時根據(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
模式工具欄的基類,后端需要通過canvas
對象處理工具欄按鈕事件、覆蓋構(gòu)造方法初始化工具欄、覆蓋save_figure()
等方法。matplotlib.backends.backend_qt5.NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar)
類:定義了QT后端默認(rèn)模式工具欄的具體實現(xiàn)。matplotlib.backend_bases.FigureCanvasBase
類:canvas
對象的基類,通過toolbar
屬性與工具欄進(jìn)行連接。matplotlib.backend_bases.NavigationToolbar2(canvas).toolitems
屬性:定義了默認(rè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ù)源碼可知,列表中每個元組為工具項定義,元組的四個元素分別表示按鈕名稱、按鈕提示文本、按鈕圖像、按鈕對應(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ā)工具欄工具項按鈕而產(chǎn)生的動作。matplotlib.backend_tools.ToolBase
類:所有工具欄工具項的基類,所有工具項均由matplotlib.backend_managers.ToolManager
實例化。matplotlib.backend_tools.default_tools
變量:字典類型,實例化基于matplotlib.backend_tools.ToolBase
類定義的內(nèi)置工具項。matplotlib.backend_tools.default_toolbar_tools
變量:嵌套列表,以類似格式[[分組1, [工具1, 工具2 ...]], [分組2, [...]]]
定義工具欄布局。matplotlib.backend_tools.add_tools_to_container
函數(shù):設(shè)置toolbarmanager
模式默認(rè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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python3.10及以上版本編譯安裝ssl模塊的詳細(xì)過程
最近搞安裝ssl模塊每天都弄到很晚,所以這里給大家整理下,這篇文章主要給大家介紹了關(guān)于python3.10及以上版本編譯安裝ssl模塊的詳細(xì)過程,文中介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05Python設(shè)計實現(xiàn)的計算器功能完整實例
這篇文章主要介紹了Python設(shè)計實現(xiàn)的計算器功能,結(jié)合完整實例形式分析了Python3.5實現(xiàn)計算器功能的正則、字符串及數(shù)值運算等相關(guān)操作技巧,需要的朋友可以參考下2017-08-08python數(shù)據(jù)處理和數(shù)據(jù)清洗的示例詳解
數(shù)據(jù)清洗是指發(fā)現(xiàn)并糾正數(shù)據(jù)文件中可識別的錯誤的最后一道程序,包括檢查數(shù)據(jù)一致性,處理無效值和缺失值等,數(shù)據(jù)清洗與處理的目的是提高數(shù)據(jù)的質(zhì)量,提高實驗結(jié)果的可靠度,本文給大家介紹了python數(shù)據(jù)處理和數(shù)據(jù)清洗的示例,需要的朋友可以參考下2024-08-08使用Plotly Dash進(jìn)行儀表板設(shè)計的步驟和技巧
Plotly Dash 是一個基于 Python 的開源框架,可以幫助你快速而靈活地構(gòu)建交互式儀表板,本文將介紹使用 Plotly Dash 創(chuàng)建儀表板的步驟和一些技巧,并附上代碼實例來演示每個步驟,需要的朋友可以參考下2024-05-05用python打開攝像頭并把圖像傳回qq郵箱(Pyinstaller打包)
這篇文章主要介紹了用python打開攝像頭并把圖像傳回qq郵箱,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Python?from?import導(dǎo)包ModuleNotFoundError?No?module?named
最近在執(zhí)行python腳本時,from?import的模塊沒有被加載進(jìn)來,找不到module,這篇文章主要給大家介紹了關(guān)于Python?from?import導(dǎo)包ModuleNotFoundError?No?module?named找不到模塊問題的解決辦法,需要的朋友可以參考下2022-08-08