Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:22:53+00:00 2026-06-17T04:22:53+00:00

For a demonstration of a Graph algorithm i need to draw a networkx graph

  • 0

For a demonstration of a Graph algorithm i need to draw a networkx graph to a Tkinter Canvas and be able to modify that graph (and the plot) at runtime.

I have pieced together the following code (I hope it is the minimal code leading to my problem, but I’m new to this so I’m not sure):

import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import Tkinter as Tk
import networkx as nx
from tkMessageBox import showinfo

root = Tk.Tk()
root.wm_title("Animated Graph embedded in TK")
root.wm_protocol('WM_DELETE_WINDOW', root.quit())

f = plt.figure(figsize=(5,4))
a = f.add_subplot(111)
plt.axis('off')

# the networkx part
G=nx.complete_graph(5)
nx.draw_networkx(G,pos=nx.spring_layout(G),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)

def next_graph():
    if G.order():
        a.cla()
        G.remove_node(G.nodes()[-1])
        nx.draw(G, pos=nx.circular_layout(G), ax=a)
        canvas.draw()

b = Tk.Button(root, text="next",command=next_graph)
b.pack()  

Tk.mainloop()

My problem now is this:
The first display of the graph is like I want it (backgroundcolor-wise), but after you first click ‘Next’ the backgroundcolor of the graph changes to white. I have tried changing the background color of the figure and the canvas.
I don’t even know what brings that change about, I think it is simply drawing to the same canvas twice.

How can I modify the code to have the graph always have the same background color?

On an unrelated note: the root.quit() I added does not help in ending the application properly. This might be stupid on my side, but what did go wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:22:54+00:00Added an answer on June 17, 2026 at 4:22 am

    I think you are very close. If you use nx.draw_networkx() in your event loop then it works (turn off the axis there too).

    Here is your example with those modifications and also with a single layout computed at the beginning that is reused in the loop:

    import matplotlib
    matplotlib.use('TkAgg')
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    import matplotlib.pyplot as plt
    import Tkinter as Tk
    import networkx as nx
    from tkMessageBox import showinfo
    
    root = Tk.Tk()
    root.wm_title("Animated Graph embedded in TK")
    # Quit when the window is done
    root.wm_protocol('WM_DELETE_WINDOW', root.quit)
    
    f = plt.figure(figsize=(5,4))
    a = f.add_subplot(111)
    plt.axis('off')
    
    # the networkx part
    G=nx.complete_graph(5)
    pos=nx.circular_layout(G)
    nx.draw_networkx(G,pos=pos,ax=a)
    xlim=a.get_xlim()
    ylim=a.get_ylim()
    
    
    
    # a tk.DrawingArea
    canvas = FigureCanvasTkAgg(f, master=root)
    canvas.show()
    canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
    
    def next_graph():
        if G.order():
            a.cla()
            G.remove_node(G.nodes()[-1])
            nx.draw_networkx(G, pos, ax=a)
            a.set_xlim(xlim)
            a.set_ylim(ylim)
            plt.axis('off')
            canvas.draw()
    
    b = Tk.Button(root, text="next",command=next_graph)
    b.pack()
    
    Tk.mainloop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're writting a Canvas demonstration Real Time Strategy (RTS) game in HTML5/JavaScript and have
Recently I saw demonstration of image distortion algorithm that preserves some objects in image.
I have a WPF application that is made exclusively for demonstration purposes about how
For a demonstration, I want to draw a cube with canvas in HTML5. This
For demonstration purpose, I have list with has a list: > (setf x (list
I am trying to re-create a TypeLoadException for demonstration purposes, so I have a
I have the TwitterOAuth class and demonstration running on a local development server and
My supervisor at the office tells me that he saw a demonstration with a
I have seen JavaScript written like this (it was at a demonstration, and I
Assuming you have a hypothetical enum in java like this (purely for demonstration purposes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.