TKinter Matplotlib 图不显示
TKinter Matplotlib plot not displaying
我正在尝试制作一个 TKinter 应用程序,它显示使用 networkx
制作的图形,使用 FigureCanvasTkAgg
,但是它没有显示该图,只显示一个空白的白色方块。这是我的程序:
root = Tk()
root.wm_protocol('WM_DELETE_WINDOW', root.quit)
canvas = FigureCanvasTkAgg(fig1, master=root)
#canvas.show()
canvas.get_tk_widget().place(relx=.5,rely=.4,anchor="center")
root.title("Item Search")
root.title_font = font.Font(family='Helvetica', size=30, weight="bold", slant="italic")
root.configure(background="#00FFFF")
label = Label(root, text = "Item Search", bg="#236B8E",fg="#67C8FF", height=2, width=root.winfo_height(),font=("Courier", 25))
label.pack(side=TOP,fill=X)
button1 = Button(root, text = "Load User Items", command = lambda:get_user_items(button2),height=2,width=11)
button1.place(relx=.5,rely=.7,anchor="center")
button2 = Button(root, text="Find the Shortest way", comman = find_shortest_way, height=2,width=15,state="disabled")
button2.place(relx=.5,rely=.9,anchor="center")
init()
init()函数:
item_db = pd.read_csv("all_items.csv")
item_dict={}
for shelf in item_db:
for item in item_db[shelf]:
if shelf != np.nan:
item_dict[item] = int(shelf)
store.add_node(0,pos=(1,1))
store.add_node(1,pos=(1,0.5))
store.add_node(2,pos=(1,0))
store.add_node(3,pos=(2,0.5))
store.add_node(4,pos=(2,0))
store.add_node(5,pos=(3,1))
store.add_node(6,pos=(3.5,0.5))
store.add_node(7,pos=(4,0))
store.add_node(8,pos=(4,2))
store.add_node(9,pos=(5,2))
store.add_node(10,pos=(5,0))
store.add_edge(0,1,weight=1)
store.add_edge(1,2,weight=1)
store.add_edge(1,3,weight=1)
store.add_edge(3,4,weight=1)
store.add_edge(3,7,weight=2)
store.add_edge(3,6,weight=1)
store.add_edge(3,5,weight=2)
store.add_edge(5,6,weight=1)
store.add_edge(5,8,weight=2)
store.add_edge(6,8,weight=3)
store.add_edge(7,10,weight=1)
store.add_edge(8,9,weight=1)
store.add_edge(8,7,weight=4)
pos=nx.get_node_attributes(store,'pos')
nx.draw(store,pos,with_labels=True)
labels = nx.get_edge_attributes(store,'weight')
nx.draw_networkx_edge_labels(store,pos,edge_labels=labels)
plt.axis('off')
fig1.canvas.draw()
如何显示剧情?我做错了什么?
在这个最小的示例中,我必须使用 nx.draw(..., ax=fig.gca() )
来显示图形
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import networkx as nx
root = tkinter.Tk()
fig = plt.Figure()
#sub = fig.add_subplot('111')
canvas = FigureCanvasTkAgg(fig, master=root)
#canvas.draw()
canvas.get_tk_widget().pack() #fill='both', expand=True)
G = nx.dodecahedral_graph()
ax = fig.gca() # it can gives list of `ax` if there is more subplots.
nx.draw(G, ax=ax)
tkinter.mainloop()
使用其他标准图显示 NetworkX 的其他示例。
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import networkx as nx
root = tkinter.Tk()
fig = plt.Figure()
sub1 = fig.add_subplot('311')
sub2 = fig.add_subplot('312')
sub3 = fig.add_subplot('313')
canvas = FigureCanvasTkAgg(fig, master=root)
#canvas.draw()
canvas.get_tk_widget().pack() #fill='both', expand=True)
data = [1, 3, 2, 4]
sub1.plot(data)
G = nx.dodecahedral_graph()
nx.draw(G, ax=sub2)
data = [1, 3, 2, 4]
sub3.plot(data)
tkinter.mainloop()
我正在尝试制作一个 TKinter 应用程序,它显示使用 networkx
制作的图形,使用 FigureCanvasTkAgg
,但是它没有显示该图,只显示一个空白的白色方块。这是我的程序:
root = Tk()
root.wm_protocol('WM_DELETE_WINDOW', root.quit)
canvas = FigureCanvasTkAgg(fig1, master=root)
#canvas.show()
canvas.get_tk_widget().place(relx=.5,rely=.4,anchor="center")
root.title("Item Search")
root.title_font = font.Font(family='Helvetica', size=30, weight="bold", slant="italic")
root.configure(background="#00FFFF")
label = Label(root, text = "Item Search", bg="#236B8E",fg="#67C8FF", height=2, width=root.winfo_height(),font=("Courier", 25))
label.pack(side=TOP,fill=X)
button1 = Button(root, text = "Load User Items", command = lambda:get_user_items(button2),height=2,width=11)
button1.place(relx=.5,rely=.7,anchor="center")
button2 = Button(root, text="Find the Shortest way", comman = find_shortest_way, height=2,width=15,state="disabled")
button2.place(relx=.5,rely=.9,anchor="center")
init()
init()函数:
item_db = pd.read_csv("all_items.csv")
item_dict={}
for shelf in item_db:
for item in item_db[shelf]:
if shelf != np.nan:
item_dict[item] = int(shelf)
store.add_node(0,pos=(1,1))
store.add_node(1,pos=(1,0.5))
store.add_node(2,pos=(1,0))
store.add_node(3,pos=(2,0.5))
store.add_node(4,pos=(2,0))
store.add_node(5,pos=(3,1))
store.add_node(6,pos=(3.5,0.5))
store.add_node(7,pos=(4,0))
store.add_node(8,pos=(4,2))
store.add_node(9,pos=(5,2))
store.add_node(10,pos=(5,0))
store.add_edge(0,1,weight=1)
store.add_edge(1,2,weight=1)
store.add_edge(1,3,weight=1)
store.add_edge(3,4,weight=1)
store.add_edge(3,7,weight=2)
store.add_edge(3,6,weight=1)
store.add_edge(3,5,weight=2)
store.add_edge(5,6,weight=1)
store.add_edge(5,8,weight=2)
store.add_edge(6,8,weight=3)
store.add_edge(7,10,weight=1)
store.add_edge(8,9,weight=1)
store.add_edge(8,7,weight=4)
pos=nx.get_node_attributes(store,'pos')
nx.draw(store,pos,with_labels=True)
labels = nx.get_edge_attributes(store,'weight')
nx.draw_networkx_edge_labels(store,pos,edge_labels=labels)
plt.axis('off')
fig1.canvas.draw()
如何显示剧情?我做错了什么?
在这个最小的示例中,我必须使用 nx.draw(..., ax=fig.gca() )
来显示图形
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import networkx as nx
root = tkinter.Tk()
fig = plt.Figure()
#sub = fig.add_subplot('111')
canvas = FigureCanvasTkAgg(fig, master=root)
#canvas.draw()
canvas.get_tk_widget().pack() #fill='both', expand=True)
G = nx.dodecahedral_graph()
ax = fig.gca() # it can gives list of `ax` if there is more subplots.
nx.draw(G, ax=ax)
tkinter.mainloop()
使用其他标准图显示 NetworkX 的其他示例。
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import networkx as nx
root = tkinter.Tk()
fig = plt.Figure()
sub1 = fig.add_subplot('311')
sub2 = fig.add_subplot('312')
sub3 = fig.add_subplot('313')
canvas = FigureCanvasTkAgg(fig, master=root)
#canvas.draw()
canvas.get_tk_widget().pack() #fill='both', expand=True)
data = [1, 3, 2, 4]
sub1.plot(data)
G = nx.dodecahedral_graph()
nx.draw(G, ax=sub2)
data = [1, 3, 2, 4]
sub3.plot(data)
tkinter.mainloop()