关于ZAKER 融媒体解决方案 合作 加入

用于多标签绘图的 python matplotlib 和 PyQT – 导 .

CocoaChina 10-21

我创建了一个 qt 应用程序 , 可用于在多个选项卡中显示 matplotlib 数字 . 现在我正在尝试使用标准的 matplotlib 导航工具栏来处理各个选项卡中的所有数字 . 到目前为止 , 我只是设法让它在其中一个数字中工作 , 但不是全部 .

这是代码:

from PyQt4 import QtCorefrom PyQt4 import QtGui as qtfrom matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvasfrom matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbarfrom matplotlib.figure import Figureimport itertoolsclass MultiTabNavTool ( NavigationToolbar ) : #==================================================================================================== def __init__ ( self, canvases, tabs, parent=None ) : self.canvases = canvases self.tabs = tabs NavigationToolbar.__init__ ( self, canvases [ 0 ] , parent ) #==================================================================================================== def get_canvas ( self ) : return self.canvases [ self.tabs.currentIndex ( ) ] def set_canvas ( self, canvas ) : self._canvas = canvas canvas = property ( get_canvas, set_canvas ) class MplMultiTab ( qt.QMainWindow ) : #==================================================================================================== def __init__ ( self, parent=None, figures=None, labels=None ) : qt.QMainWindow.__init__ ( self, parent ) self.main_frame = qt.QWidget ( ) self.tabWidget = qt.QTabWidget ( self.main_frame ) self.create_tabs ( figures, labels ) # Create the navigation toolbar, tied to the canvas self.mpl_toolbar = MultiTabNavTool ( self.canvases, self.tabWidget, self.main_frame ) self.vbox = vbox = qt.QVBoxLayout ( ) vbox.addWidget ( self.mpl_toolbar ) vbox.addWidget ( self.tabWidget ) self.main_frame.setLayout ( vbox ) self.setCentralWidget ( self.main_frame ) #==================================================================================================== def create_tabs ( self, figures, labels ) : if labels is None: labels = [ ] figures = [ Figure ( ) ] if figures is None else figures #initialise with empty figure in first tab if no figures provided self.canvases = [ self.add_tab ( fig, lbl ) for ( fig, lbl ) in itertools.zip_longest ( figures, labels ) ] #==================================================================================================== def add_tab ( self, fig=None, name=None ) : '''dynamically add tabs with embedded matplotlib canvas with this function.''' # Create the mpl Figure and FigCanvas objects. if fig is None: fig = Figure ( ) ax = fig.add_subplot ( 111 ) canvas = fig.canvas if fig.canvas else FigureCanvas ( fig ) canvas.setParent ( self.tabWidget ) canvas.setFocusPolicy ( QtCore.Qt.ClickFocus ) #self.tabs.append ( tab ) name = 'Tab %i'% ( self.tabWidget.count ( ) +1 ) if name is None else name self.tabWidget.addTab ( canvas, name ) return canvas

一个基本的用法示例是:

import numpy as npimport matplotlib.pyplot as pltx = np.linspace ( 1, 2*np.pi, 100 ) figures = [ ] for i in range ( 1,3 ) : fig, ax = plt.subplots ( ) y = np.sin ( np.pi*i*x ) +0.1*np.random.randn ( 100 ) ax.plot ( x,y ) figures.append ( fig ) app = qt.QApplication ( sys.argv ) ui = MplMultiTab ( figures=figures ) ui.show ( ) app.exec_ ( )

是否有任何 matplotlib 忍者可能知道如何让导航工具栏与多个数字画布一起玩?

最佳答案

我认为您可以为每个画布创建工具栏 , 并在 tabs.currentTab 更改时显示 / 隐藏它们:

class MultiTabNavTool ( qt.QWidget ) : def __init__ ( self, canvases, tabs, parent=None ) : qt.QWidget.__init__ ( self, parent ) self.canvases = canvases self.tabs = tabs self.toolbars = [ NavigationToolbar ( canvas, parent ) for canvas in self.canvases ] vbox = qt.QVBoxLayout ( ) for toolbar in self.toolbars: vbox.addWidget ( toolbar ) self.setLayout ( vbox ) self.switch_toolbar ( ) self.tabs.currentChanged.connect ( self.switch_toolbar ) def switch_toolbar ( self ) : for toolbar in self.toolbars: toolbar.setVisible ( False ) self.toolbars [ self.tabs.currentIndex ( ) ] .setVisible ( True )

以上内容由"CocoaChina"上传发布 查看原文
相关标签 标签appui画布