Thank you,
Lars Ruoff
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()
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
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.
from matplotlib.figure import Figure
from matplotlib.pyplot import figure
f = Figure(figsize=(5,4), dpi=100)
f = figure(figsize=(5,4), dpi=100)