电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

python matplotlib工具欄源碼探析二之添加、刪除內(nèi)置工具項(xiàng)的案例

瀏覽:25日期:2022-06-27 09:48:09

從matplotlib工具欄源碼探析一(禁用工具欄、默認(rèn)工具欄和工具欄管理器三種模式的差異)一文可知matplotlib內(nèi)置實(shí)現(xiàn)了多個(gè)工具項(xiàng)的實(shí)現(xiàn),而默認(rèn)工具欄中的工具項(xiàng)只是其中的一部分,有沒有方法直接管理工具欄,添加、刪除內(nèi)置工具項(xiàng)?

matplotlib內(nèi)置的工具項(xiàng)

由源碼可知,matplotlib.backend_tools.default_tools變量為字典類型,實(shí)例化了基于matplotlib.backend_tools.ToolBase類定義的內(nèi)置工具項(xiàng)。

源碼

default_tools = {’home’: ToolHome, ’back’: ToolBack, ’forward’: ToolForward, ’zoom’: ToolZoom, ’pan’: ToolPan, ’subplots’: ’ToolConfigureSubplots’, ’save’: ’ToolSaveFigure’, ’grid’: ToolGrid, ’grid_minor’: ToolMinorGrid, ’fullscreen’: ToolFullScreen, ’quit’: ToolQuit, ’quit_all’: ToolQuitAll, ’allnav’: _ToolEnableAllNavigation, ’nav’: _ToolEnableNavigation, ’xscale’: ToolXScale, ’yscale’: ToolYScale, ’position’: ToolCursorPosition, _views_positions: ToolViewsPositions, ’cursor’: ’ToolSetCursor’, ’rubberband’: ’ToolRubberband’, ’help’: ’ToolHelp’, ’copy’: ’ToolCopyToClipboard’, }

驗(yàn)證

import matplotlib.pyplot as pltimport matplotlib as mplfrom pprint import pprintplt.rcParams[’toolbar’] = ’toolmanager’fig = plt.gcf()pprint(mpl.backend_tools.default_tools)

輸出

{’allnav’: <class ’matplotlib.backend_tools._ToolEnableAllNavigation’>, ’back’: <class ’matplotlib.backend_tools.ToolBack’>, ’copy’: ’ToolCopyToClipboard’, ’cursor’: ’ToolSetCursor’, ’forward’: <class ’matplotlib.backend_tools.ToolForward’>, ’fullscreen’: <class ’matplotlib.backend_tools.ToolFullScreen’>, ’grid’: <class ’matplotlib.backend_tools.ToolGrid’>, ’grid_minor’: <class ’matplotlib.backend_tools.ToolMinorGrid’>, ’help’: ’ToolHelp’, ’home’: <class ’matplotlib.backend_tools.ToolHome’>, ’nav’: <class ’matplotlib.backend_tools._ToolEnableNavigation’>, ’pan’: <class ’matplotlib.backend_tools.ToolPan’>, ’position’: <class ’matplotlib.backend_tools.ToolCursorPosition’>, ’quit’: <class ’matplotlib.backend_tools.ToolQuit’>, ’quit_all’: <class ’matplotlib.backend_tools.ToolQuitAll’>, ’rubberband’: ’ToolRubberband’, ’save’: ’ToolSaveFigure’, ’subplots’: ’ToolConfigureSubplots’, ’viewpos’: <class ’matplotlib.backend_tools.ToolViewsPositions’>, ’xscale’: <class ’matplotlib.backend_tools.ToolXScale’>, ’yscale’: <class ’matplotlib.backend_tools.ToolYScale’>, ’zoom’: <class ’matplotlib.backend_tools.ToolZoom’>}

使用工具欄管理器管理內(nèi)置工具項(xiàng)

由源碼可知默認(rèn)工具欄模式toolbar2模式?jīng)]有提供添加、刪除工具項(xiàng)的接口。因此,管理工具欄需要使用工具欄管理器模式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)工具欄。使用系統(tǒng)函數(shù)實(shí)現(xiàn)添加工具項(xiàng)

根據(jù)源碼可知,matplotlib.backend_tools.add_tools_to_container函數(shù)可以設(shè)置toolbarmanager模式默認(rèn)工具欄。

案例

案例說明:為工具欄添加全屏切換工具項(xiàng)。

import matplotlib.pyplot as pltimport matplotlib as mplplt.rcParams[’toolbar’] = ’toolmanager’fig = plt.gcf()# 通過mpl.backend_tools.add_tools_to_container函數(shù)添加工具項(xiàng)mpl.backend_tools.add_tools_to_container(fig.canvas.manager.toolbar, tools=[[’foo’, [ ’fullscreen’]]])plt.show()

案例解析:add_tools_to_container函數(shù)有兩個(gè)參數(shù)container和tools,由源碼可知container參數(shù)的值應(yīng)為fig.canvas.manager.toolbar,tools參數(shù)按照[[分組1, [工具1, 工具2 ...]], [分組2, [...]]]格式取值。

python matplotlib工具欄源碼探析二之添加、刪除內(nèi)置工具項(xiàng)的案例

使用工具欄管理器實(shí)現(xiàn)添加、刪除內(nèi)置工具項(xiàng)

根據(jù)源碼可知:

添加內(nèi)置工具項(xiàng)有兩種方法

toolbar對(duì)象可以通過add_tool方法添加內(nèi)置工具項(xiàng),參數(shù)為name和tool,name為工具項(xiàng)的名稱,tool為添加的工具項(xiàng)對(duì)應(yīng)的類或者字符串。 toolbar對(duì)象可以通過add_toolitem方法添加內(nèi)置工具項(xiàng),參數(shù)為name、group、 position、 image_file、 description和 toggle,name為工具項(xiàng)的名稱,group為工具項(xiàng)所在組,position為工具項(xiàng)在組中的位置,取值為列表索引,一般取-1即在所在組末尾追加,設(shè)置為0即在所在組的首位,image_file為工具項(xiàng)圖像,值為字符串,description為工具項(xiàng)描述, toggle為是否為切換式工具項(xiàng),布爾值。 刪除內(nèi)置工具項(xiàng)有兩種方法 toolbar對(duì)象可以通過remove_toolitem方法刪除內(nèi)置工具項(xiàng),參數(shù)為name,即工具項(xiàng)的名稱。 toolmanager對(duì)象可以通過remove_tool方法刪除內(nèi)置工具項(xiàng),參數(shù)為name,即工具項(xiàng)的名稱。案例

案例說明:刪除向前工具項(xiàng),添加全屏切換工具項(xiàng)。

import matplotlib.pyplot as pltimport matplotlib as mplplt.rcParams[’toolbar’] = ’toolmanager’fig = plt.gcf()fig.canvas.manager.toolmanager.remove_tool(’forward’)fig.canvas.manager.toolbar.remove_toolitem(’back’)fig.canvas.manager.toolbar.add_tool(’quit’, ’foo’)fig.canvas.manager.toolbar.add_toolitem(’fullscreen’, ’foo’, -1,’fullscreen’,’fullscreen’,False) plt.show()

python matplotlib工具欄源碼探析二之添加、刪除內(nèi)置工具項(xiàng)的案例

總結(jié)

通過工具欄管理器添加、刪除內(nèi)置工具項(xiàng)的方法很多種,需要注意調(diào)用對(duì)象、方法、參數(shù),閱讀下面的matplotlib源碼可能會(huì)有所啟發(fā)。

相關(guān)源碼

matplotlib.backends.backend_qt5模塊

class FigureManagerQT(FigureManagerBase): self.toolbar = self._get_toolbar(self.canvas, self.window) if self.toolmanager: backend_tools.add_tools_to_manager(self.toolmanager) if self.toolbar: backend_tools.add_tools_to_container(self.toolbar) if self.toolbar: self.window.addToolBar(self.toolbar) tbs_height = self.toolbar.sizeHint().height() else: tbs_height = 0

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

class ToolbarQt(ToolContainerBase, QtWidgets.QToolBar): def __init__(self, toolmanager, parent): ToolContainerBase.__init__(self, toolmanager) QtWidgets.QToolBar.__init__(self, parent) self.setAllowedAreas( QtCore.Qt.TopToolBarArea | QtCore.Qt.BottomToolBarArea) message_label = QtWidgets.QLabel('') message_label.setAlignment( QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter) message_label.setSizePolicy( QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Ignored)) self._message_action = self.addWidget(message_label) self._toolitems = {} self._groups = {} def add_toolitem( self, name, group, position, image_file, description, toggle): button = QtWidgets.QToolButton(self) if image_file: button.setIcon(NavigationToolbar2QT._icon(self, image_file)) button.setText(name) if description: button.setToolTip(description) def handler(): self.trigger_tool(name) if toggle: button.setCheckable(True) button.toggled.connect(handler) else: button.clicked.connect(handler) self._toolitems.setdefault(name, []) self._add_to_group(group, name, button, position) self._toolitems[name].append((button, handler)) def _add_to_group(self, group, name, button, position): gr = self._groups.get(group, []) if not gr: sep = self.insertSeparator(self._message_action) gr.append(sep) before = gr[position] widget = self.insertWidget(before, button) gr.insert(position, widget) self._groups[group] = gr def toggle_toolitem(self, name, toggled): if name not in self._toolitems: return for button, handler in self._toolitems[name]: button.toggled.disconnect(handler) button.setChecked(toggled) button.toggled.connect(handler) def remove_toolitem(self, name): for button, handler in self._toolitems[name]: button.setParent(None) del self._toolitems[name] def set_message(self, s): self.widgetForAction(self._message_action).setText(s

matplotlib.backend_tools模塊

def add_tools_to_container(container, tools=default_toolbar_tools): ''' Add multiple tools to the container. Parameters ---------- container : Container `backend_bases.ToolContainerBase` object that will get the tools added. tools : list, optional List in the form ``[[group1, [tool1, tool2 ...]], [group2, [...]]]`` where the tools ``[tool1, tool2, ...]`` will display in group1. See `add_tool` for details. ''' for group, grouptools in tools: for position, tool in enumerate(grouptools): container.add_tool(tool, group, position)

def add_tools_to_manager(toolmanager, tools=default_tools): ''' Add multiple tools to a `.ToolManager`. Parameters ---------- toolmanager : `.backend_managers.ToolManager` Manager to which the tools are added. tools : {str: class_like}, optional The tools to add in a {name: tool} dict, see `add_tool` for more info. ''' for name, tool in tools.items(): toolmanager.add_tool(name, tool)

到此這篇關(guān)于python matplotlib工具欄源碼探析二之添加、刪除內(nèi)置工具項(xiàng)的案例的文章就介紹到這了,更多相關(guān)python matplotlib內(nèi)置工具項(xiàng)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 标策网-专注公司商业知识服务、助力企业发展| 博博会2021_中国博物馆及相关产品与技术博览会【博博会】 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 全国国际化学校_国际高中招生_一站式升学择校服务-国际学校网 | 罗氏牛血清白蛋白,罗氏己糖激酶-上海嵘崴达实业有限公司 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 数显恒温培养摇床-卧式/台式恒温培养摇床|朗越仪器 | 电子万能试验机_液压拉力试验机_冲击疲劳试验机_材料试验机厂家-济南众标仪器设备有限公司 | 高低温试验房-深圳高低温湿热箱-小型高低温冲击试验箱-爱佩试验设备 | 志高装潢官网-苏州老房旧房装修改造-二手房装修翻新 | 双段式高压鼓风机-雕刻机用真空泵-绍兴天晨机械有限公司 | 六维力传感器_三维力传感器_二维力传感器-南京神源生智能科技有限公司 | 北京征地律师,征地拆迁律师,专业拆迁律师,北京拆迁律师,征地纠纷律师,征地诉讼律师,征地拆迁补偿,拆迁律师 - 北京凯诺律师事务所 | 鼓风干燥箱_真空烘箱_高温干燥箱_恒温培养箱-上海笃特科学仪器 | [品牌官网]贵州遵义双宁口腔连锁_贵州遵义牙科医院哪家好_种植牙_牙齿矫正_原华美口腔 | 压装机-卧式轴承轮轴数控伺服压装机厂家[铭泽机械] | 吨袋包装机|吨包秤|吨包机|集装袋包装机-烟台华恩科技 | 迪威娱乐|迪威娱乐客服|18183620002| 智能监控-安防监控-监控系统安装-弱电工程公司_成都万全电子 | 空调风机,低噪声离心式通风机,不锈钢防爆风机,前倾皮带传动风机,后倾空调风机-山东捷风风机有限公司 | 软文发布平台 - 云软媒网络软文直编发布营销推广平台 | 液压扳手-高品质液压扳手供应商 - 液压扳手, 液压扳手供应商, 德国进口液压拉马 | 聚合甘油__盐城市飞龙油脂有限公司| 土壤墒情监测站_土壤墒情监测仪_土壤墒情监测系统_管式土壤墒情站-山东风途物联网 | 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 | 航空铝型材,7系铝型材挤压,硬质阳*氧化-余润铝制品 | 锂电池砂磨机|石墨烯砂磨机|碳纳米管砂磨机-常州市奥能达机械设备有限公司 | 杭州用友|用友软件|用友财务软件|用友ERP系统--杭州协友软件官网 | 挤出机_橡胶挤出机_塑料挤出机_胶片冷却机-河北伟源橡塑设备有限公司 | 电动打包机_气动打包机_钢带捆扎机_废纸打包机_手动捆扎机 | 防爆电机_防爆电机型号_河南省南洋防爆电机有限公司 | 扬尘在线监测系统_工地噪声扬尘检测仪_扬尘监测系统_贝塔射线扬尘监测设备「风途物联网科技」 | 婚博会2024时间表_婚博会门票领取_婚博会地址-婚博会官网 | 焊缝跟踪系统_激光位移传感器_激光焊缝跟踪传感器-创想智控 | 西装定制/做厂家/公司_西装订做/制价格/费用-北京圣达信西装 | 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | 全自动翻转振荡器-浸出式水平振荡器厂家-土壤干燥箱价格-常州普天仪器 | 天津货架厂_穿梭车货架_重型仓储货架_阁楼货架定制-天津钢力仓储货架生产厂家_天津钢力智能仓储装备 | 全自动端子机|刺破式端子压接机|全自动双头沾锡机|全自动插胶壳端子机-东莞市傅氏兄弟机械设备有限公司 | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 |