Hi Ala, On Mon, Jul 20, 2009 at 11:44 AM, Ala Al-Shaibani<shaib...@ymail.com> wrote: > Hello everyone. > A users on the networkx mailing list posted this example which is a > modification of the matplotlib-pyqt4 implementation which plots a networkx > graph. > The problem I am facing with it is that two plot windows open instead of > one, one is empty and the other contains the graph. > I can't really put my finger as to why two windows open rather than just one > pyqt window with the plot. Any suggestions would be appreciated. > Screenshot of the two plot windows: > http://img259.imageshack.us/img259/8722/picture1ahr.jpg > Code: > > #!/usr/bin/env python > > # embedding_in_qt4.py --- Simple Qt4 application embedding matplotlib > canvases > > # > > # Copyright (C) 2005 Florent Rougon > > # 2006 Darren Dale > > # > > # This file is an example program for matplotlib. It may be used and > > # modified with no restriction; raw copies as well as modified versions > > # may be distributed without limitation. > > import sys, os, random > > from PyQt4 import QtGui, QtCore > > from numpy import arange, sin, pi > > from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as > FigureCanvas > > from matplotlib.figure import Figure > > import networkx as nx > > progname = os.path.basename(sys.argv[0]) > > progversion = "0.1" > > class MyMplCanvas(FigureCanvas): > > """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, > etc.).""" > > def __init__(self, parent=None, width=5, height=4, dpi=100): > > fig = Figure(figsize=(width, height), dpi=dpi) > > self.axes = fig.add_subplot(111) > > # We want the axes cleared every time plot() is called > > self.axes.hold(False) > > self.compute_initial_figure() > > # > > FigureCanvas.__init__(self, fig) > > self.setParent(parent) > > FigureCanvas.setSizePolicy(self, > > QtGui.QSizePolicy.Expanding, > > QtGui.QSizePolicy.Expanding) > > FigureCanvas.updateGeometry(self) > > def compute_initial_figure(self): > > pass > > class MyStaticMplCanvas(MyMplCanvas): > > """Simple canvas with a sine plot.""" > > def compute_initial_figure(self): > > G=nx.path_graph(10) > > pos=nx.spring_layout(G) > > nx.draw(G,pos,ax=self.axes)
I think this must be the problem. It looks like networkx is providing or building upon the pylab interface, which takes care of creating windows for you. So you are mixing with the object oriented interface from MyMplCanvas, which should not be done. If you want to embed matplotlib in a GUI application, you need to stick with the object oriented interface. Something like: self.axes.some_plot_command(...) Darren ------------------------------------------------------------------------------ Enter the BlackBerry Developer Challenge This is your chance to win up to $100,000 in prizes! For a limited time, vendors submitting new applications to BlackBerry App World(TM) will have the opportunity to enter the BlackBerry Developer Challenge. See full prize details at: http://p.sf.net/sfu/Challenge _______________________________________________ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users