5
votes

How to mark edges in a graph constructed using python and xdot

I have figured out a way to construct graph in python using dot language.

import sys
import threading
import time
import networkx as nx 
import xdot 
import gtk

class MyClass(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self) 
        self.graph = nx.DiGraph(name="my_tree")
        self.xdot = xdot.DotWindow()
        self.xdot.connect('destroy', gtk.main_quit)

    def run(self):
        gtk.main()

    def add_node(self, parent, node):

        self.graph.add_edge(parent, node)
        self.xdot.set_dotcode(nx.to_agraph(self.graph).to_string())
        self.xdot.show_all()

def main(argv=None):

    gtk.gdk.threads_init()
    my_class = MyClass()
    my_class.start()

    my_class.add_node('operating_system', 'file_mgmt')
    time.sleep(1.5)

if __name__ == "__main__":
    sys.exit(main())

The above program will create a graph with an edge between operating system and file management concepts automatically. The concepts will be marked in the ellipses.

My problem is to mark a "subclass" of label on that edge using python language so that the relationship is clear between the concepts

Is there any mechanism available to do so ?

2
Would you please explain what you mean by "mark the edges"? - tripleee
Also, pleaae fix the code formatting (edit the question, select the code block, and hit ctrl-K; that should hopefully suffice). - tripleee

2 Answers

9
votes

You can specify edge's label as a named argument to add_edge:

self.graph.add_edge(parent, node, label='subclass')
1
votes
 def add_node(self, parent, node,i):

        self.graph.add_edge(parent, node, label = i)
        self.xdot.set_dotcode(nx.to_agraph(self.graph).to_string())
        self.xdot.show_all()

so this way,i think we can dynamically pass the labels each time and capture all the relations and not just sub class of .