使用matplotlib繪圖時,在彈出的窗口中默認是有工具欄的,那么這些工具欄是如何定義的呢?
工具欄的三種模式
matplotlib的基礎配置由運行時參數(rcParams)控制,導入matplotlib時,加載matplotlibrc文件生成默認運行時參數。
查看matplotlibrc文件可知#toolbar: toolbar2 # {None, toolbar2, toolmanager},即工具欄有三種模式None、toolbar2和toolmanager,其中默認模式為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工具欄內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python matplotlib工具欄源碼探析二之添加、刪除內置工具項的案例
- python matplotlib工具欄源碼探析三之添加、刪除自定義工具項的案例詳解
- 詳解python安裝matplotlib庫三種失敗情況
- Python matplotlib讀取excel數據并用for循環畫多個子圖subplot操作