Matplotlib:拖动拖放和blitting

时间:2014-08-17 08:54:33

标签: python matplotlib blit

我创建了一个FigureCanvas,允许拖放所有Text个实例。这适用于轴内的文本,但对于轴外的文本(例如s轴标签),在拾取文本时,它会留下一条“跟踪”字样。你移动它的文本。 释放鼠标后,踪迹消失,文本处于其所需的位置,但我试图理解为什么会发生这种情况(为什么它会发生在轴外,但不在内部?)

只有当我尝试介绍' blitting'为了表现。所以我假设我在blitting中犯了一些错误。

拖放功能的代码如下。有什么想法吗?

import logging
import sys
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from PySide import QtGui
logger = logging.getLogger('colorpicker_example')
logger.setLevel(logging.DEBUG)

class JFigureCanvas(FigureCanvas):
    def __init__(self):
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)

        self._draggedArtist = None

        self.mpl_connect("pick_event", self.pck_event)
        self.mpl_connect("motion_notify_event", self.motion_event)
        self.mpl_connect("button_release_event", self.release_event)

    def pck_event(self, event):
        """ Pick event handler for text objects. If a pick event occurs, grab the artist and position."""
        if isinstance(event.artist, text.Text):
            self._draggedArtist = event.artist   

            #Get the x y position and transform it to display coords   
            x, y = self._draggedArtist.get_transform().transform_point(
                            (self._draggedArtist._x, self._draggedArtist._y))  
            self.startPos = (x, y, event.mouseevent.x, event.mouseevent.y)

            # draw everythin but the selected text and store the pixel buffer
            self._draggedArtist.set_animated(True)
            self.draw()
            self.background = self.copy_from_bbox(self.figure.bbox)

            # redraw the text
            self.figure.draw_artist(self._draggedArtist)

            # blit the redrawn area
            self.blit(self.figure.bbox)

    def motion_event(self, event):
        """ Motion event handler. If there is an artist stored, moved it with the mouse and redraw"""
        if self._draggedArtist:
            x0, y0, xpress, ypress = self.startPos
            dx = event.x - xpress
            dy = event.y - ypress
            canvasLoc = (x0 + dx, y0 + dy)
            newPos = self._draggedArtist.get_transform().inverted().transform_point(canvasLoc)
            self._draggedArtist.set_position(newPos)

            # Restore the background
            self.restore_region(self.background)

            #redraw the text
            self.figure.draw_artist(self._draggedArtist)

            # blit the redrawn area 
            self.blit(self.figure.bbox)

    def release_event(self, event):
        " If the mouse is released, release any artist"
        if self._draggedArtist:
            self._draggedArtist.set_animated(False)
        self.background = None
        self._draggedArtist = None
        self.draw()

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        chart = JFigureCanvas()

        # Put some text in the axes
        chart.axes.text(0.5, 0.5, "Test", picker = True)

        self.setCentralWidget(chart)

if __name__ == '__main__':

    logging.basicConfig()
    app = QtGui.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    app.exec_()

操作中的问题的屏幕截图(您可以看到在左上方的轴外移动时留下的'轨迹')

Blitting problem

0 个答案:

没有答案