Rainer
unread,Aug 24, 2010, 10:01:04 AM8/24/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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_()