Chen Xu
Chen Xu

Reputation: 352

Displaying a directed graph from edge set using pydot

I want to create a directed graph using pydot and I have a ready-made edge set as well. The type of edge set can be a list or a tuple which is no matter, because I can construct the set of edge according to my requirement in advance, just like [(1,2),(2,3),(3,2)] or ((1,2),(2,3),(3,2)).

I initialize a pydot object g as follows :

g = pydot.Dot()
g.set_type('digraph')

After that, I find there has not a function likes add_edge_from, only has a add_edge function for g. Is it means I must generate edge one by one??

Perhaps I could create graph from edge set by following ways at the beginning.

g=pydot.graph_from_edges(edge_set)

But I find it will produce a undirected graph:(

By the way, I try to realize the graph using networkx and success. However, its garish and circuitous showing presentation mode, which attach more importance to edge other than node (Sorry it is just my opinion, and of course you may disagree with it), is not accord with I want in this case now.

So is anyone have any ideas or advises for me? Is there off-the-shelf method I can use? Thanks for any help !

Upvotes: 3

Views: 2317

Answers (2)

Pritam Dodeja
Pritam Dodeja

Reputation: 326

@chen-xu, you are on the right track with creating a digraph object. What you have to do to get it to be a directed graph is to create node objects using pydot.Node(), and then create an edge object where you specify the source and destination nodes, and then add this edge object to your graph.
Find an example below:

import pydot                                                                                                                                                                
stackoverflow_graph = pydot.Dot("stackoverflow_graph", graph_type="digraph", bgcolor="white")                                                                               
child_node = pydot.Node(name='Childish Node')                                                                                                                               
parent_node = pydot.Node(name='Grownup Node')                                                                                                                               
directed_edge = pydot.Edge(src=parent_node, dst=child_node, label="Node, I am your father!")                                                                                
stackoverflow_graph.add_edge(directed_edge)                                                                                                                                 
stackoverflow_graph.write_jpeg('./stackoverflow_graph.jpg')           

Directed graph with misdirected humor.

Upvotes: 2

Aric
Aric

Reputation: 25289

In [1]: import networkx as nx

In [2]: G = nx.DiGraph([(1,2),(2,3),(3,2)])

In [3]: from networkx.drawing.nx_pydot import write_dot

In [4]: write_dot(G,'file.dot')

In [5]: !dot -Tpng file.dot >file.png

enter image description here

Upvotes: 1

Related Questions