0

I am trying to make a TKinter app that displays a graph made with networkx, using FigureCanvasTkAgg, But the plot it not displayed, only a blank white square is displayed. Here is my program:

    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() function:

    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()

How do I display the plot? what am I doing wrong?

1
  • 1
    I'm not sure if you don't have to use nx.draw(... ax=...) to inform NetworkX that you already create matplotlib object and it has to use it to display graph. Commented Mar 7, 2020 at 12:34

1 Answer 1

1

In this minimal example I have to use nx.draw(..., ax=fig.gca() ) to display graph

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()

enter image description here

Other example which display NetworkX with other standard plots.

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()

enter image description here

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

3 Comments

thanks, just a simple mistake. tkinter is good library but difficult to work with as so few tutorials and the official documents is really bad
I added example which draws NetworkX graph between normal plots.
ya, got it. thank you very much. I was searching all over the internet for such small mistake. I just didn't knew to pass the ax.

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.