5

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 Answers2

9

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

self.graph.add_edge(parent, node, label='subclass')
max taldykin
  • 12,459
  • 5
  • 45
  • 64
  • That worked out!! so is to possible to generalize this such that label can be a dynamic value which keeps of adding new relations between the concepts ? – Jayakrishnan B May 29 '14 at 13:03
1
 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 .