Reputation: 4332
I have a list of items that relate to each other, I know they end up building a graph but the data is not sorted in any way.
PMO-100 -> SA-300
SA-100 -> SA-300
SA-100 -> SA-200
PMO-100 -> SA-100
In python examples for graphViz api I realize that you can pretty much generate a graph if you know it's top node and can run down the levels and build all the relations.
Since I'm getting an assorted list (I only know that it is a graph since all relations are figured from a single point) - is there a way to build a graph on top of this data?
Update: the first comment to this question identifies I haven't correctly explained what I'm after. Indeed the following code gets you a proper graph:
gr = pydot.Dot(graph_type='digraph')
for link in graph.links:
edge = pydot.Edge(link.outwardIssue.key, link.inwardIssue.key)
gr.add_edge(edge)
gr.write('graph.png',format='png')
my question really is - how can I color-rode individual nodes or change type of arrow for a specific edge?
Upvotes: 0
Views: 3293
Reputation: 20341
Try out pygraph package, it produces a directed graph based on relation statements (data). No matter if the data are sorted or not.
So (as in your case) if we have the following data (in a triple relation), it is easy to produce the corresponding graph:
s1 = "PMO-100 -> SA-300"
s2 = "SA-100 -> SA-300"
s3 = "SA-100 -> SA-200"
s4 = "PMO-100 -> SA-100"
from pygraph.dgraph import PyGraph
g = PyGraph()
g.add_relation(s1)
g.add_relation(s2)
g.add_relation(s3)
g.add_relation(s4)
g.draw_graph(label=False)
It will produce:
Or you can even read the relations from a file.
Say, you have data.txt
:
PMO-100 -> SA-300
SA-100 -> SA-300
SA-100 -> SA-200
PMO-100 -> SA-100
It is even easier in 4 loc,:
from pygraph.dgraph import PyGraph
g = PyGraph()
g.file_relations("data.txt")
g.draw_graph(label=False)
In both ways the result is the same:
Upvotes: 0
Reputation: 1638
How to change color of nodes:
node.attr["color"] = 'red'
to change the shape of arrowhead, same method
edge.attr["arrowhead"] = "..."
Upvotes: 0
Reputation: 7099
There is no need to identify a top node for graphviz. Just add all Nodes and edges and let it do the rest. For example:
import pydot
graph = pydot.Dot('graphname', graph_type='digraph')
pmo100 = pydot.Node("PMO-100")
sa300 = pydot.Node("SA-300")
sa100 = pydot.Node("SA-100")
sa200 = pydot.Node("SA-200")
graph.add_edge(pydot.Edge(pmo100, sa300))
graph.add_edge(pydot.Edge(sa100, sa300))
graph.add_edge(pydot.Edge(sa100, sa200))
graph.add_edge(pydot.Edge(pmo100, sa100))
graph.write_png('example1_graph.png')
This will result in the following image:
You can find more info at http://pythonhaven.wordpress.com/2009/12/09/generating_graphs_with_pydot/
Upvotes: 1