Make a visual representation of a graph
I have a list of edges.
(1,2),(1,3),(1,4),(1,5),(1,6),(2,4),(2,7),(3,4),(3,7),(4,5),(4,7开发者_JAVA技巧),(5,6),(6,7)
How can I get an image of this graph?
It should be automatic, because there are over 9000(not kidding) those lists.
I have always used graphviz for this sort of stuff.
You can draw it with Python and networkx
.
import networkx
import pylab
edges = [(1,2),(1,3),(1,4),(1,5),(1,6),(2,4),(2,7),(3,4),(3,7),(4,5),(4,7),(5,6),(6,7)]
G = networkx.Graph(data=edges)
networkx.draw(G)
pylab.show()
You should read pylab
's documentation on how to save the graph as an image without using the GUI. You can use ast.literal_eval
to parse the original lists. For example, if it stored as one graph on a line in a file, you can do:
with open('edges.txt') as f:
for line in f:
edges = list(ast.literal_eval(line))
# drawing goes here
精彩评论