使用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
、toolbar2
和toolmanager
,其中默認(rèn)模式為toolbar2
。
工具欄模式切換
通過(guò)類似語(yǔ)句plt.rcParams['toolbar'] = 'None'
可控制工具欄的模式。
需要注意的是plt.rcParams['toolbar'] = 'None'
應(yīng)當(dāng)放置在圖像實(shí)例化之前。
None
模式:禁用工具欄。
plt.rcParams['toolbar'] = 'None'
![](/d/20211017/1801465ed0fff21ed7f4855430d45f00.gif)
toolbar2
模式:默認(rèn)工具欄布局。
plt.rcParams['toolbar'] = 'toolbar2'
![](/d/20211017/6db07136f13094a296aa95352f2aa476.gif)
toolmanager
模式:工具欄布局模式與toolbar2
模式稍有不同。
plt.rcParams['toolbar'] = 'toolmanager'
![](/d/20211017/d7c2751cab64244930125d8b07fc7db1.gif)
工具欄模式切換原理
和工具欄相關(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操作