large graph visualization with python and networkx

CodeKingPlusPlus picture CodeKingPlusPlus · Jun 29, 2013 · Viewed 24.7k times · Source

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!

Answer

Vikram picture Vikram · Jun 30, 2013
 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.