Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
892 views
in Technique[技术] by (71.8m points)

large graph visualization with python and networkx

I am having trouble with large graph visualization in python and networkx. The graph is wish to visualize is directed, and has an edge and vertex set size of 215,000 From the documenation (which is linked at the top page) it is clear that networkx supports plotting with matplotlib and GraphViz. In matplotlib and networkx the drawing is done as follows:

import 
networkx as nx
import matplotlib.pyplot as plt
#Let g be a graph that I created
nx.draw(g)

I get a memory error after nx.draw(g), afterwards you would normally do plt.show() or plt.[some_function] to save the file in a format for efficient and so forth.

Next I tried GraphViz. From the wikipedia page the dot format is used for directed graphs and I created a dot file:

nx.write_dot(g, "g.dot")

This worked well and I had a dot file in my current directory that is 12 megabytes. Next I ran the dot program (part of graphviz to create a postscript file):

dot -Tps g.dot -o g.ps

This slows down my computer, runs for a few minutes and then display Killed in the terminal. So it never could execute... While reading the documentation for graphviz it seems that only undirected graphs were supported for large graph visualization.

Question: With these two unsuccessful attempts can anyone show me how to visualize my large graph using python and networkx with about 215,000 vertices and 215,000 edges? I suspect as with Graphviz I will have to output into an intermediate format (although this shouldn't be that hard it won't be as easy as a builtin function) and then use another tool to read the intermediate format and then output a visualization.

So, I am looking for the following:

  1. Output graph from networkx into an intermediate format
  2. With new package/software/tool (ideally python-interactive) read intermediate format and visualize the large graph

If you need more information let me know!

question from:https://stackoverflow.com/questions/17381006/large-graph-visualization-with-python-and-networkx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
 from matplotlib import pylab
 import networkx as nx

 def save_graph(graph,file_name):
    #initialze Figure
    plt.figure(num=None, figsize=(20, 20), dpi=80)
    plt.axis('off')
    fig = plt.figure(1)
    pos = nx.spring_layout(graph)
    nx.draw_networkx_nodes(graph,pos)
    nx.draw_networkx_edges(graph,pos)
    nx.draw_networkx_labels(graph,pos)

    cut = 1.00
    xmax = cut * max(xx for xx, yy in pos.values())
    ymax = cut * max(yy for xx, yy in pos.values())
    plt.xlim(0, xmax)
    plt.ylim(0, ymax)

    plt.savefig(file_name,bbox_inches="tight")
    pylab.close()
    del fig

#Assuming that the graph g has nodes and edges entered
save_graph(g,"my_graph.pdf")

#it can also be saved in .svg, .png. or .ps formats

This answers your first issue. Networkx does not have the facility to zoom into nodes. Use Gephi for this functionality. Gephi accepts an edge list in CSV format and produces a visualization, where zooming can be done interactively.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...