2

I have Networkx 1.6 and Matplotlib 1.1.0 on Windows this is my code:

self.figure = Figure()
self.axes = self.figure.add_subplot(1,1,1)
self.canvas = FigureCanvas(self, -1, self.figure)
G = nx.Graph()
G.add_node(6)
pos = nx.spring_layout(G)
nx.draw(G, pos, ax = self.axes)

And I get the error:

File "C:\Python27\lib\site-packages\matplotlib\axes.py, line 1374, 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

Does Anyone know how to fix it?

3
  • there are examples of how to draw networkx graphs on the networkx website: networkx.lanl.gov/examples/drawing Commented Dec 27, 2011 at 13:01
  • this is only a non-functional part of your code. Please post an example of a functional, minimal code that allows us to reproduce your problem Commented Dec 27, 2011 at 16:45
  • It's probably some issue with the figure canvas. But joaquin is right - we need to see the functional code that reproduces the problem. Commented Dec 27, 2011 at 18:17

4 Answers 4

2

For matplotlib 1.0+ don't use Figure(), use pyplot.figure(). Figure() makes a Figure but doesn't register it with the figManager inside pyplot, pyplot.figure() does.

In the draw functions they get the figure by calling gcf(), and gcf() returns the current figure or creates a new one if none is present.

Later a call to sci() will try to verify, by calling gca(), that the positions (collection) you applied to the draw function is indeed already registered with the axis, but since you have a new figure, and thus no axis, it will raise the exception.

I will call this a matplotlib bug.

I haven't read the changes notes for matplotlib, it may be described there. I found out by debugging the matplotlib code.

Sign up to request clarification or add additional context in comments.

Comments

1

Is there a way to dock/embed the pyplot.show() command in a tkinter panel/main window ? Or does it always pop up in it's own window ?

def Embedded_Graph(Parent, G):
 Parent.figure = Figure()
 Parent.axe = Parent.figure.add_subplot(1,1,1)
 pos = nx.spring_layout(G)
 nx.draw(G, pos)
 pyplot.show()

Comments

1

@Carel, I hope you found what you search for. If not, here is example how to embed networkx graph on Tkinter canvas:

def embed_graph(G):
    pos = nx.spring_layout(G)
    nx.draw(G, pos)
    canvas = FigureCanvasTkAgg(plt.figure(1), master=self)
    canvas.show()
    canvas.get_tk_widget().pack(side="top")

Comments

0

I am not sure what you want to draw exactly but you get a plot of your node doing:

    self.figure = Figure()
    self.axe = self.figure.add_subplot(1,1,1)
    G = nx.Graph()
    G.add_node(6)
    pos = nx.spring_layout(G)
    nx.draw(G, pos)
    pyplot.show()

Thus, removing the apparently correct ax parameter allows drawing the figure. I found a post here showing the same error related to the ax parameter. It seems it was working on mpl 0.99 but not in mpl 1.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.