日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

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

瀏覽:144日期: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)只是其中的一部分,有沒(méi)有方法直接管理工具欄,添加、刪除內(nèi)置工具項(xiàng)?

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

由源碼可知,matplotlib.backend_tools.default_tools變量為字典類(lèi)型,實(shí)例化了基于matplotlib.backend_tools.ToolBase類(lèi)定義的內(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)類(lèi):工具欄容器的基類(lèi),定義了工具欄編輯的方法。構(gòu)造函數(shù)參數(shù)為toolmanager,表示工具欄容器容納的工具欄。 matplotlib.backend_managers.ToolManager(figure=None)類(lèi):管理用戶(hù)觸發(fā)工具欄工具項(xiàng)按鈕而產(chǎn)生的動(dòng)作。matplotlib.backend_tools.ToolBase類(lèi):所有工具欄工具項(xiàng)的基類(lèi),所有工具項(xiàng)均由matplotlib.backend_managers.ToolManager實(shí)例化。 matplotlib.backend_tools.default_tools變量:字典類(lèi)型,實(shí)例化基于matplotlib.backend_tools.ToolBase類(lèi)定義的內(nèi)置工具項(xiàng)。 matplotlib.backend_tools.default_toolbar_tools變量:嵌套列表,以類(lèi)似格式[[分組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)工具欄。

案例

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

import matplotlib.pyplot as pltimport matplotlib as mplplt.rcParams[’toolbar’] = ’toolmanager’fig = plt.gcf()# 通過(guò)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ì)象可以通過(guò)add_tool方法添加內(nèi)置工具項(xiàng),參數(shù)為name和tool,name為工具項(xiàng)的名稱(chēng),tool為添加的工具項(xiàng)對(duì)應(yīng)的類(lèi)或者字符串。 toolbar對(duì)象可以通過(guò)add_toolitem方法添加內(nèi)置工具項(xiàng),參數(shù)為name、group、 position、 image_file、 description和 toggle,name為工具項(xiàng)的名稱(chē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ì)象可以通過(guò)remove_toolitem方法刪除內(nèi)置工具項(xiàng),參數(shù)為name,即工具項(xiàng)的名稱(chēng)。 toolmanager對(duì)象可以通過(guò)remove_tool方法刪除內(nèi)置工具項(xiàng),參數(shù)為name,即工具項(xiàng)的名稱(chēng)。案例

案例說(shuō)明:刪除向前工具項(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é)

通過(guò)工具欄管理器添加、刪除內(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)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
激情五月色综合国产精品| 综合色就爱涩涩涩综合婷婷| 久久国产日韩欧美精品| 男女男精品视频网| 爽好久久久欧美精品| 国产手机视频一区二区| 在线亚洲成人| 免费不卡在线观看| 一区二区三区网站| 亚洲九九精品| 日本三级亚洲精品| 亚洲精品麻豆| 久久精品999| 久久精品免视看国产成人| 成人国产综合| 91精品国产调教在线观看| 亚洲va在线| 国产一区白浆| 亚洲不卡视频| 国产精品亚洲成在人线| 麻豆91精品91久久久的内涵| 成人在线免费观看网站| 六月婷婷综合| 国产高清久久| 亚洲精选成人| 免费日韩成人| 都市激情国产精品| 欧美二三四区| 国产一区91| 国产九九精品| 日本黄色精品| 亚洲精品成人| 91精品日本| 精品视频久久| 欧美精品激情| 欧美一级二级三级视频| 国产精品99一区二区三| 国产精品88久久久久久| 亚洲精品看片| 精品中文在线| 亚洲一级特黄| 六月婷婷一区| 国产精区一区二区| 欧美日韩尤物久久| 亚洲影视一区二区三区| 九九久久国产| 国产精品日本欧美一区二区三区| 欧美三级第一页| 成人羞羞在线观看网站| 巨乳诱惑日韩免费av| 国产精品流白浆在线观看| 三上悠亚国产精品一区二区三区| 亚洲欧美日韩国产一区二区| 国产精品va| 精品一区在线| 国产精成人品2018| 久久青草久久| 日韩成人在线看| 日韩三区免费| 日本免费一区二区视频| 亚洲电影有码| 日本午夜精品久久久| 亚洲精品在线影院| 国产日韩欧美一区| 尤物网精品视频| 麻豆视频久久| 中文字幕亚洲影视| 蜜臀久久精品| 91久久精品无嫩草影院| 激情综合网站| 精品久久福利| 久久香蕉精品| 久久电影tv| 青青青国产精品| 午夜精品免费| 成人在线视频区| 日韩国产欧美视频| 欧美日韩国产免费观看视频| 国产精品丝袜在线播放| 亚洲主播在线| 99精品电影| 欧美激情日韩| 日韩黄色av| 视频一区免费在线观看| 欧美香蕉视频| 久久精品国产精品亚洲毛片| 男女男精品网站| 欧美~级网站不卡| 国产精品国产三级国产在线观看| 日韩精品91亚洲二区在线观看| 亚洲午夜久久久久久尤物| 国产在线日韩精品| 国产精区一区二区| 日本综合精品一区| 日韩亚洲在线| 91精品蜜臀一区二区三区在线| 国产剧情一区二区在线观看| 蜜臀久久久久久久| 日韩午夜av在线| 久久婷婷av| 波多野结衣久久精品| 欧美影院精品| 亚洲精品伊人| 亚洲三级国产| 蜜桃久久精品一区二区| 亚洲激情不卡| 欧美在线资源| 在线日韩中文| 久久精品导航| 日韩免费视频| 中文字幕一区久| 在线亚洲人成| 麻豆精品在线视频| 久久国产婷婷国产香蕉| 日韩高清成人在线| 亚洲精品系列| 热久久国产精品| 免费在线观看成人| 中文一区二区| 麻豆精品91| 蜜臀久久99精品久久久久久9| 亚洲精品99| 蜜桃视频在线网站| 99成人超碰| 国产婷婷精品| 亚洲一区国产一区| 欧美日韩一区二区综合| 日韩av不卡一区二区| 综合日韩在线| 日本中文字幕一区二区视频| 亚洲欧美专区| 国产模特精品视频久久久久| 欧美1区免费| 欧美成人综合| 在线成人直播| 三级一区在线视频先锋| 综合国产视频| 青青草国产精品亚洲专区无| 国产精品一区二区三区www| 国产一区 二区| 国产黄色精品| 国产在线不卡一区二区三区 | 国产精品一页| 久久影视三级福利片| 国产传媒av在线| 黄色在线观看www| 久久激情网站| 男人的天堂久久精品| 欧美一区成人| 久久免费精品| 色爱av综合网| 水蜜桃久久夜色精品一区的特点| 日韩精品高清不卡| 美日韩一区二区三区| 国产精品高颜值在线观看| 亚洲午夜电影| 日本在线不卡视频一二三区| 国产精品毛片视频| 日韩欧美字幕| 免费看精品久久片| 欧美日韩一区二区国产 | 国产成人精品一区二区三区在线| 成人久久久久| 中文字幕av一区二区三区四区| 日韩成人精品一区二区三区| 精品国产午夜肉伦伦影院| 九九在线精品| 国产精品亚洲综合在线观看| 久久免费黄色| 亚洲v天堂v手机在线| 美腿丝袜亚洲一区| 欧美日韩国产传媒| 国产欧美日韩精品高清二区综合区 | 大香伊人久久精品一区二区| 国产中文一区| 911亚洲精品| 99免费精品| 91成人福利| 婷婷成人在线| 欧美三级第一页| 久久麻豆精品| 国产探花一区| 精品中文字幕一区二区三区av| 7777精品| 1000部精品久久久久久久久| 欧美日韩伊人| 五月婷婷六月综合| 国产精品久久久久久模特| 亚洲大片在线| 国产精品伦一区二区| 偷拍欧美精品| 精品一区二区三区免费看 | 日韩欧美二区| 日韩激情综合| 欧美粗暴jizz性欧美20| 国产高清亚洲| 蜜桃伊人久久| 精品丝袜在线| 国产福利一区二区三区在线播放| 91久久黄色|