Menu DaniWeb
Log In Sign Up
  • Read
  • Contribute
Navigation
  • Forum Categories
  • laptop Hardware/Software
  • code Programming
  • live_tv Digital Media
  • local_cafe Community Center
  • Latest Content
  • Newest Topics
  • Latest Topics
  • forumLatest Posts
  • Top Tags
  • Topics Feed
  • Social
  • Top Members
  • DaniWeb Premium
  • Newsletter Archive
  • Community Rules
  • Connect API
  • Forum API Docs
  • DaniWeb Ads
  • Terms of Service
  • Privacy Policy
  • FAQ
  • About Us
  • Contact Us
© 2021 DaniWeb® LLC
  1. Home
  2. Programming Forum
  3. Software Development Forum
  4. Code Snippet Repository
  5. Reusable Code Snippet

Link any objects in a graph, and draw the graph.

10 Years Ago Gribouillis

This snippet defines 3 functions to create in a few seconds an image of
a graph containing arbitrary python objects. It uses the module pygraphviz
(available at http://pypi.python.org/pypi/pygraphviz/).

See the example use case at the end of the module.

graph image python
mygraph.png 5.33 KB
884 Views
About the Author
Member Avatar Gribouillis 1,391 Programming Explorer Team Colleague

Mind explorer.

# module fastgraph
# created by Gribouillis for the python forum at www.daniweb.com
# November 9, 2010
# Licence: public domain

"""This module defines 3 functions (node, edge and graph) to help
create a graph (a pygraphviz.AGraph instance) linking arbitrary
python objects.

This graph can be saved in various image formats, and in dot format.
"""

import pygraphviz

def node(x):
    "helper to create graphs"
    return (x,)

def edge(x, y):
    "helper to create graphs"
    return (x, y)

def graph(items, tolabel, **kwd):
    """Create a pygraphviz AGraph instance

    @ items - a sequence of node(obj), or edge(obj, obj) items (obj can be any python object)
    @ tolabel - a function tolabel(obj) -> str which converts an object to string
    @ **kwd - additional keyword arguments for AGraph.__init__
    """
    names = dict()
    the_graph = pygraphviz.AGraph(**kwd)
    for uple in (tuple(t) for t in items):
        for obj in uple:
            if not obj in names:
                names[obj] = "n%d" % len(names)
                the_graph.add_node(names[obj], label=tolabel(obj), shape="box")
        if len(uple) == 2:
            the_graph.add_edge(*(names[obj] for obj in uple))
    return the_graph

if __name__ == "__main__":
    
    def my_items():
        """Exemple generator of graph items. Our graph contains string here"""
        for x in "abcde":
            yield node(x)
        for s in "ab ac bd ea da".split():
            x, y = iter(s)
            yield edge(x, y)
            
    def my_label(x):
        "Create a label for graph objects"
        return x.upper()
    
    g = graph(my_items(), my_label)
    g.draw('mygraph.png', prog='circo') # prog can be neato|dot|twopi|circo|fdp|nop
Member Avatar
woooee 814 Nearly a Posting Maven
10 Years Ago

Consider posting this in the tutorials forum also so we can find it later
http://www.daniweb.com/tutorials/forum114.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.

Sign Up — It's Free!
Related Topics
  • Member Avatar [Prob]Download image with python 2
  • Member Avatar Loading image from parent! 3
  • Member Avatar Assembly Language CAN be easy to learn! 4
  • Member Avatar RegularExpression => Graphing Data 2
  • Member Avatar wxPython canvas drawing save 0
  • Member Avatar Airline Ticket Reservations System 11
  • Member Avatar Images not displaying in tkinter 4
  • Member Avatar wxPython GUI problem 3
  • Member Avatar Traversing a Huffman Tree 8
  • Member Avatar Graph class in wxWidgets 1
  • Member Avatar adding text to image: python 1
  • Member Avatar Calling a Hashtable from another class. 4
  • Member Avatar Transparent Image (Python,Tkinter) 4
  • Member Avatar Python Question 1
  • Member Avatar Executing a batch file from java code. 5
  • Member Avatar [PYGAME] headless application image problem 0
  • Member Avatar Changing an image 2
  • Member Avatar How to build a tree structure by tokenizing the wkt string stream 1
  • Member Avatar My experiences building a small app with Python. 12
  • Member Avatar Convert python to c# 1
Not what you need?

Reach out to all the awesome people in our software development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Start New Topic
Topics Feed
add_comment Reply to this Topic
This topic is old! No one has contributed to this discussion in over 10 years. Are you sure you have something valuable to add to revive the existing conversation? Consider starting a new topic instead. Otherwise, please be thoughtful, detailed and courteous, and adhere to our posting rules.
  • Edit
  • Preview
H1 H2

Insert Code Block

Share Post