Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to Label the edges of a graph using python and graphviz

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 ?

like image 720
Jayakrishnan B Avatar asked May 29 '14 04:05

Jayakrishnan B


People also ask

How do I use graphviz?

Create a graph object, assemble the graph by adding nodes and edges, and retrieve its DOT source code string. Save the source code to a file and render it with the Graphviz installation of your system. Use the view option/method to directly inspect the resulting (PDF, PNG, SVG, etc.) file with its default application.

What is Python graphviz?

Graphviz is an open-source graph visualisation software. The graphviz package, which works under Python 3.7+ in Python, provides a pure-Python interface to this software. This package allows to create both undirected and directed graphs using the DOT language.


1 Answers

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

self.graph.add_edge(parent, node, label='subclass')
like image 131
max taldykin Avatar answered Sep 27 '22 19:09

max taldykin