Hi,I spend some time writing up the question below on Stackoverflow which immediately was closed as a duplicate of other posts. To my best knowledge, these posts did not answer my questions - so I'll try my luck here instead:
I am using the qt backengine for setting up a QWidget that embeds a matplotlib scene. When the widget is closed it appears that many of the matplotlib objects that were part of the plot still are alive in Python space.
My question is basically the following: What actions should I take to clean up the figure and axes objects etc. that were part of the plot widget? The qt backend comes with a figure manager, but it appears a little unclear how it should be used.
I have attached a small unit test example that sets up a plot. When the plot appears, just close it, and the test will garbage collects the plot, and then display info of the matplotlib objects that are still alive in Python space. Clearly both the path of the plot, and several Bbox objects are still referenced.
Our current unit test suite contains almost 10000 GUI tests and its imperative that proper object space clean up is done after each test. Any help is much appreciated.
Best regards, Mads -- +-----------------------------------------------------+ | Mads Ipsen | +----------------------+------------------------------+ | Gåsebæksvej 7, 4. tv | | | DK-2500 Valby | phone: +45-29716388 | | Denmark | email: mads.ip...@gmail.com | +----------------------+------------------------------+
import sys import gc import numpy import unittest from PyQt4 import QtCore, QtGui from matplotlib.figure import Figure from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg # Need a Qt app to handle events QT_APP = QtGui.QApplication(sys.argv) class Plot2D(QtGui.QWidget): """ 2D Plot class based on a matplotlib canvas. """ def __init__(self, parent=None, testing=False): """ Constructor. @param parent : Parent widget. @param testing : Set to True if testing """ # Initialize base class QtGui.QWidget.__init__(self, parent) # Set a layout layout = QtGui.QVBoxLayout() self.setLayout(layout) # Set up figure self._figure = Figure() self._canvas = FigureCanvasQTAgg(self._figure) # Add widgets to the layout layout.addWidget(self._canvas) # Draw something self._axes = self._figure.add_subplot(111) self.draw() def draw(self): """ Redraws a figure. Added for unit testing purposes but may also be used for inspiration on how to make a plot. """ str = '1 2 3 4' data = map(int, str.split()) x = range(len(data)) # clear the axes and redraw the plot anew self._axes.clear() self._axes.bar( left=x, height=data, width=8.0/ 100.0, align='center', alpha=0.44, picker=5) t = numpy.arange(0.0, 3.0, 0.01) s = numpy.sin(2*numpy.pi*t) self._axes.plot(t, s, picker=5) self._axes.set_title('This is a title') self._axes.set_xlabel('Clock is ticking') self._axes.set_ylabel('Time is running') self._canvas.draw() class Plot2DTest(unittest.TestCase): def setUp(self): self._plot = Plot2D() def tearDown(self): self._plot.close() del self._plot gc.collect() @classmethod def tearDownClass(cls): QtGui.qApp.processEvents() print print 'Performing tear down for', cls print 'Active matplotlib objects\n--------------:' objs = [o for o in gc.get_objects() if 'matplotlib' in str(type(o))] for o in objs: print o def testConstruction(self): """ Test that class instantiation suceeds """ self._plot.show() QtGui.qApp.exec_() if __name__ == '__main__': unittest.main()
------------------------------------------------------------------------------ Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS, MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft MVPs and experts. ON SALE this month only -- learn more at: http://p.sf.net/sfu/learnnow-d2d
_______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users