Hello,
I have adopted blit animation example [1] to use it inside PySide Qt widget. But I found a little problem. When I follow the example [1] and use
self.blit(self.axes.bbox) call in ‘draw_event’ callback, the ugly black frame is
appeared during the application window zoom (changing size of the window). But when I remove this line everything works well. But I does not understand why?
Please, see the code below:
import sys
from PySide import QtCore, QtGui
from matplotlib import rcParams;
rcParams[‘backend.qt4’] = ‘PySide’
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as Canvas
from matplotlib.figure import Figure
from matplotlib.lines import Line2D
class MatplotlibWidget(Canvas):
def init(self, parent = None):
# figure
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.axes.plot([0.0, 1.0])
# markers
self.marker_id = None
self.x = [0.3, 0.7]
self.y = [0.5, 0.5]
self.markers = Line2D(self.x, self.y, ls = ' ', marker = 'o',
picker = 5, animated = True)
self.axes.add_line(self.markers)
# initialization with current figure
super(MatplotlibWidget, self).__init__(self.figure)
self.setParent(parent)
self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
self.updateGeometry()
# events
self.mpl_connect('draw_event', self.on_draw)
self.mpl_connect('pick_event', self.on_picked)
self.mpl_connect('button_release_event', self.on_button_released)
self.mpl_connect('motion_notify_event', self.on_motion)
def on_draw(self, event):
self.background = self.copy_from_bbox(self.axes.bbox)
self.axes.draw_artist(self.markers)
# this line of code cause ugly black frame over the plot during the
# window zoom (changing app window size)
self.blit(self.axes.bbox)
def on_picked(self, event):
self.marker_id = event.ind
def on_button_released(self, event):
if event.button != 1:
return
self.marker_id = None
def on_motion(self, event):
if event.inaxes is None:
return
if event.button != 1:
return
if self.marker_id is None:
return
x, y = event.xdata, event.ydata
self.x[self.marker_id] = x
self.y[self.marker_id] = y
self.markers.set_data([self.x, self.y])
self.restore_region(self.background)
self.axes.draw_artist(self.markers)
self.blit(self.axes.bbox)
class ApplicationWindow(QtGui.QMainWindow):
def init(self):
super(ApplicationWindow, self).init()
self.setWindowTitle(“Animation Demo”)
self.setCentralWidget(MatplotlibWidget())
def main():
app = QtGui.QApplication(sys.argv)
appWindow = ApplicationWindow()
appWindow.show()
sys.exit(app.exec_())
if name == ‘main’:
main()
My environment:
Ubuntu 12.04, python-matplotlib 1.1.1~rc1+git20120423-0ubuntu1
[1] http://matplotlib.org/examples/event_handling/poly_editor.html
Sincerely,
Alexander