在Qt4中以编程方式更改matplotlib工具栏模式

时间:2013-07-17 22:05:55

标签: matplotlib pyqt toolbar

我正在使用matplotlib版本1.2.1,Qt4.7,python 2.7构建应用程序。

我想修改matplotlib工具栏以添加另一个可检查按钮,该按钮设置用于选择数据点以响应拾取事件的模式。它几乎都可以工作,除了选择缩放或平移导航按钮时不发送拾取事件。

我正在寻找的是一种以编程方式关闭平移和缩放模式的方法。我想我可以通过将工具栏平移和缩放按钮的选中状态设置为False来完成此操作。这似乎有效(例如,如果选中缩放按钮,设置为false会使其显示为未选中)。但是它不会更改画布的模式 - 光标仍然是缩放光标,并且拾取事件不会触发。

以下代码演示了这一点:

import sys, os, math
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg
from matplotlib.figure import Figure

class NavigationToolbar( NavigationToolbar2QTAgg ):

    picked=pyqtSignal(int,name='picked')

    def __init__(self, canvas, parent ):
        NavigationToolbar2QTAgg.__init__(self,canvas,parent)
        self.clearButtons=[]
        # Search through existing buttons
        # next use for placement of custom button
        next=None
        for c in self.findChildren(QToolButton):
            if next is None:
                next=c
            # Don't want to see subplots and customize
            if str(c.text()) in ('Subplots','Customize'):
                c.defaultAction().setVisible(False)
                continue
            # Need to keep track of pan and zoom buttons
            # Also grab toggled event to clear checked status of picker button
            if str(c.text()) in ('Pan','Zoom'):
                c.toggled.connect(self.clearPicker)
                self.clearButtons.append(c)
                next=None

        # create custom button
        pm=QPixmap(32,32)
        pm.fill(QApplication.palette().color(QPalette.Normal,QPalette.Button))
        painter=QPainter(pm)
        painter.fillRect(6,6,20,20,Qt.red)
        painter.fillRect(15,3,3,26,Qt.blue)
        painter.fillRect(3,15,26,3,Qt.blue)
        painter.end()
        icon=QIcon(pm)
        picker=QAction("Pick",self)
        picker.setIcon(icon)
        picker.setCheckable(True)
        picker.setToolTip("Pick data point")
        self.picker = picker
        button=QToolButton(self)
        button.setDefaultAction(self.picker)

        # Add it to the toolbar, and connect up event
        self.insertWidget(next.defaultAction(),button)
        picker.toggled.connect(self.pickerToggled)

        # Grab the picked event from the canvas
        canvas.mpl_connect('pick_event',self.canvasPicked)

    def clearPicker( self, checked ):
        if checked:
            self.picker.setChecked(False)

    def pickerToggled( self, checked ):
        if checked:
            for c in self.clearButtons:
                c.defaultAction().setChecked(False)
            self.set_message('Reject/use observation')

    def canvasPicked( self, event ):
        if self.picker.isChecked():
            self.picked.emit(event.ind)

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.x=[i*0.1 for i in range(30)]
        self.y=[math.sin(x) for x in self.x]
        self.picked=[]
        self.setWindowTitle('Custom toolbar')

        self.frame = QWidget()

        self.fig = Figure((4.0, 4.0))
        self.canvas = FigureCanvasQTAgg(self.fig)
        self.canvas.setParent(self.frame)

        self.axes = self.fig.add_subplot(111)

        # Create the navigation toolbar, tied to the canvas
        # and link the clicked event
        self.toolbar = NavigationToolbar(self.canvas, self.frame)
        self.toolbar.picked.connect(self.addPoint)

        vbox = QVBoxLayout()
        vbox.addWidget(self.canvas)
        vbox.addWidget(self.toolbar)
        self.frame.setLayout(vbox)
        self.setCentralWidget(self.frame)
        self.draw()

    def draw(self):
        while self.axes.lines:
            self.axes.lines[0].remove()
        self.axes.plot(self.x,self.y,'b+',picker=5)
        xp=[self.x[i] for i in self.picked] 
        yp=[self.y[i] for i in self.picked] 
        self.axes.plot(xp,yp,'rs')
        self.canvas.draw()

    def addPoint(self,index):
        if index in self.picked:
            self.picked.remove(index)
        else:
            self.picked.append(index)
        self.draw()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = MainWindow()
    form.show()
    app.exec_()

1 个答案:

答案 0 :(得分:6)

如果您阅读NavigationToolbar2QT的源代码,您可以找到:

  1. _active是平移和缩放的当前状态。
  2. 致电pan()zoom()方法将切换状态。
  3. 所以,这是禁用pan& amp;变焦:

    def pickerToggled( self, checked ):
        if checked:            
            if self._active == "PAN":
                self.pan()
            elif self._active == "ZOOM":
                self.zoom()
            self.set_message('Reject/use observation')