Draw graph to Tkinter Canvas?

12,374 views
Skip to first unread message

Lars Ruoff

unread,
Jul 2, 2009, 3:24:50 PM7/2/09
to networkx...@googlegroups.com
Hello,
i have played around a bit with networkx and the matplotlib drawing
functions.
I wonder if there is a way i could draw a graph in a Tkinter canvas widget
of my application.
I.e. my application creates the Tkinter canvas and somehow passes it as an
argument to a draw function.
I didn't see this in the documentation and it is probably a question i
should address to the matplotlib people, but if any of you have experience
with interactive graph applications in Python/Tkinter, i'd be happy to hear
your advice on this.
(If not Tkinter then other widget libs would do as well)

Thank you,
Lars Ruoff

Aric Hagberg

unread,
Jul 2, 2009, 4:21:00 PM7/2/09
to networkx...@googlegroups.com

Take a look at the matplotlib examples.
http://matplotlib.sourceforge.net/examples/user_interfaces/index.html

Here is a lightly edited version of one that writes to a Tk canvas.
Aric


--

#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,
NavigationToolbar2TkAgg
from matplotlib.figure import Figure

import Tkinter as Tk
import sys

def destroy(e): sys.exit()

root = Tk.Tk()
root.wm_title("Embedding in TK")
#root.bind("<Destroy>", destroy)


f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)

######################
# the networkx part
import networkx as nx
G=nx.path_graph(8)
pos=nx.spring_layout(G)
nx.draw(G,pos,ax=a)
######################

# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

toolbar = NavigationToolbar2TkAgg( canvas, root )
toolbar.update()
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

#button = Tk.Button(master=root, text='Quit', command=sys.exit)
#button.pack(side=Tk.BOTTOM)

Tk.mainloop()

Lars Ruoff

unread,
Jul 3, 2009, 3:19:19 PM7/3/09
to networkx...@googlegroups.com
Works!
Thank you.

Lars

Lars Ruoff

unread,
Jun 29, 2012, 4:39:39 PM6/29/12
to networkx...@googlegroups.com
Hi, i don't remember what i exactly did at that time when i wrote it worked.
But now, when i run the exact above code, it gives me the following errors:

Message    File Name    Line    Position   
Traceback               
    <module>    D:\Users\Max\Projects\my4x\test\test-aric.py    27       
    draw    D:\Dev\Python27\lib\site-packages\networkx-1.7rc1-py2.7.egg\networkx\drawing\nx_pylab.py    133       
    draw_networkx    D:\Dev\Python27\lib\site-packages\networkx-1.7rc1-py2.7.egg\networkx\drawing\nx_pylab.py    266       
    draw_networkx_nodes    D:\Dev\Python27\lib\site-packages\networkx-1.7rc1-py2.7.egg\networkx\drawing\nx_pylab.py    391       
    sci    D:\Dev\Python27\lib\site-packages\matplotlib\pyplot.py    226       
    _sci    D:\Dev\Python27\lib\site-packages\matplotlib\axes.py    1374       
ValueError: Argument must be an image, collection, or ContourSet in this Axes               

Anybody experienced this and could help?

James McDermott

unread,
Jul 5, 2012, 8:57:23 AM7/5/12
to networkx...@googlegroups.com
Well, I get the same error. I bet it's a version skew problem. I'm on OSX 10.6, Python 2.6.1, NetworkX 1.7rc1, Matplotlib 1.2.x.

Googling the ValueError gives quite a few leads.

Lars Ruoff

unread,
Jul 10, 2012, 3:23:03 PM7/10/12
to networkx...@googlegroups.com
Hi,
yes, thanks.
Ok, so the bottom line is that it apparently broke with the move from matplotlib-0.99 to matplotlib-1.0.0.
But for the moment nobody knows why.

Aric Hagberg

unread,
Jul 10, 2012, 3:26:33 PM7/10/12
to networkx...@googlegroups.com
Can you run the examples from
http://matplotlib.sourceforge.net/examples/user_interfaces/index.html

That earlier code in this thread is just one of those examples with a
few lines pasted in.

Aric

Lars Ruoff

unread,
Jul 12, 2012, 3:23:21 PM7/12/12
to networkx...@googlegroups.com
On Tuesday, July 10, 2012 9:26:33 PM UTC+2, A Hagberg wrote:

Can you run the examples from
http://matplotlib.sourceforge.net/examples/user_interfaces/index.html

That earlier code in this thread is just one of those examples with a
few lines pasted in.

Aric

Hi Aric,
yes, the code from the example "embedding_in_tk.py"
(http://matplotlib.sourceforge.net/examples/user_interfaces/embedding_in_tk.html) works.

Daniel Bühler

unread,
Jan 12, 2013, 9:18:09 AM1/12/13
to networkx...@googlegroups.com
My previous post was premature, i fixed it now.

Found solution here:
And an excerpt of what goes wrong is this:

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 anew 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.

so just change
from matplotlib.figure import Figure
to
from matplotlib.pyplot import figure 

and 
f = Figure(figsize=(5,4), dpi=100)
to 
 f = figure(figsize=(5,4), dpi=100)

and the code works like a charm. 
Reply all
Reply to author
Forward
0 new messages