networkx matplotlib integration error

303 views
Skip to first unread message

Rainer

unread,
Aug 24, 2010, 10:01:04 AM8/24/10
to networkx-discuss
Hi,

this code used to work (adapted from example that is still online, see
below) ... now I get this error: "ValueError: Argument must be an
image, collection, or ContourSet in this Axes" when I am trying to
draw the graph (nx.draw(H,ax=self.axes)) ... problem is the
"ax=self.axes" part.

I get this error with networkx 1.2 and 1.4rev1939, matplotlib 1.0,
Python 2.65 on Windows XP.

Any ideas what might be causing this?

Thanks,
Rainer

class MyDynamicMplCanvas(MyMplCanvas):
"""A canvas that updates itself every second with a new plot."""
def __init__(self, *args, **kwargs):
MyMplCanvas.__init__(self, *args, **kwargs)

def computeInitialFigure(self):
H = nx.path_graph(4)
pos=nx.circular_layout(H, 2, 1)
x = random.random()
colors = ['g','g','g','g']
H.add_edges_from([(1,3), (1,2), (0,1)])
nx.draw(H,ax=self.axes)

File "C:\ProgLang\Python26\lib\site-packages\networkx\drawing
\nx_pylab.py", line 138, in draw
draw_networkx(G,pos=pos,ax=ax,**kwds)
File "C:\ProgLang\Python26\lib\site-packages\networkx\drawing
\nx_pylab.py", line 272, in draw_networkx
node_collection=draw_networkx_nodes(G, pos, **kwds)
File "C:\ProgLang\Python26\lib\site-packages\networkx\drawing
\nx_pylab.py", line 404, in draw_networkx_nodes
pylab.sci(node_collection)
File "C:\ProgLang\Python26\lib\site-packages\matplotlib\pyplot.py",
line 174, in sci
gca()._sci(im)
File "C:\ProgLang\Python26\lib\site-packages\matplotlib\axes.py",
line 1356, in _sci
"Argument must be an image, collection, or ContourSet in this
Axes")
ValueError: Argument must be an image, collection, or ContourSet in
this Axes

---------------------------------------------------------
here's the example I found online ... causes the same error:

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)
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("application main window")
self.file_menu = QtGui.QMenu('&File', self)
self.file_menu.addAction('&Quit', self.fileQuit,
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
self.menuBar().addMenu(self.file_menu)
self.help_menu = QtGui.QMenu('&Help', self)
self.menuBar().addSeparator()
self.menuBar().addMenu(self.help_menu)
self.help_menu.addAction('&About', self.about)
self.main_widget = QtGui.QWidget(self)
l = QtGui.QVBoxLayout(self.main_widget)
sc = MyStaticMplCanvas(self.main_widget, width=5, height=4,
dpi=100)
l.addWidget(sc)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
self.statusBar().showMessage("All hail matplotlib!", 2000)
def fileQuit(self):
self.close()
def closeEvent(self, ce):
self.fileQuit()
def about(self):
QtGui.QMessageBox.about(self, "About %s" % progname,
u"""%(prog)s version %(version)s
Copyright \N{COPYRIGHT SIGN} 2005 Florent Rougon, 2006 Darren Dale
This program is a simple example of a Qt4 application embedding
matplotlib
canvases.
It may be used and modified with no restriction; raw copies as well as
modified versions may be distributed without limitation."""
% {"prog": progname, "version": progversion})
qApp = QtGui.QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("%s" % progname)
aw.show()
sys.exit(qApp.exec_())
#qApp.exec_()

Aric Hagberg

unread,
Aug 24, 2010, 11:03:25 AM8/24/10
to networkx...@googlegroups.com
On Tue, Aug 24, 2010 at 8:01 AM, Rainer <rainer....@gmail.com> wrote:
> Hi,
>
> this code used to work (adapted from example that is still online, see
> below) ... now I get this error: "ValueError: Argument must be an
> image, collection, or ContourSet in this Axes" when I am trying to
> draw the graph (nx.draw(H,ax=self.axes)) ... problem is the
> "ax=self.axes" part.
>
> I get this error with networkx 1.2 and 1.4rev1939, matplotlib 1.0,
> Python 2.65 on Windows XP.
>
> Any ideas what might be causing this?

That works for me with matplotlib-0.99. So there must have been some
change in matplotlib that is causing this.

I'll see if I can track it down - so far I haven't tried using
matplotlib-1.0 so there might be other issues too.

Aric

Rainer

unread,
Aug 24, 2010, 11:43:42 AM8/24/10
to networkx-discuss
Hi Aric,

you are right ... just tried my code with matplotlib 0.99.3 ... and it
works fine!

Thanks,
Rainer

On Aug 24, 11:03 am, Aric Hagberg <ahagb...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages