Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a huge graph with networkX and matplotlib

Tags:

I am drawing a graph with around 5K nodes in it using networkX and matplotlib. The GTK window by matplotlib has tools to zoom and visualise the graph. Is there any way, I can save a magnified version for proper visualisation later?

import matplotlib.pyplot as plt import networkx as nx  pos=nx.spring_layout(G)   #G is my graph  nx.draw(G,pos,node_color='#A0CBE2',edge_color='#BB0000',width=2,edge_cmap=plt.cm.Blues,with_labels=True) #plt.show() plt.savefig("graph.png", dpi=500, facecolor='w', edgecolor='w',orientation='portrait', papertype=None, format=None,transparent=False, bbox_inches=None, pad_inches=0.1)  
like image 315
Nihar Sarangi Avatar asked Feb 22 '12 20:02

Nihar Sarangi


People also ask

Can NetworkX handle large graphs?

NX is certainly capable of handling graphs that large, however, performance will largely be a function of your hardware setup. Aric will likely give a better answer, but NX loads graphs into memory at once, so in the ranges your are describing you will need a substantial amount of free memory for it to work.

How do you visualize a NetworkX graph?

Option 1: NetworkX NetworkX has its own drawing module which provides multiple options for plotting. Below we can find the visualization for some of the draw modules in the package. Using any of them is fairly easy, as all you need to do is call the module and pass the G graph variable and the package does the rest.

Which is better iGraph or NetworkX?

NetworkX is pure Python, well documented and handles changes to the network gracefully. iGraph is more performant in terms of speed and ram usage but less flexible for dynamic networks. iGraph is a C library with very smart indexing and storage approaches so you can load pretty large graphs in ram.

Is NetworkX scalable?

NetworkX provides a standardized way for data scientists and other users of graph mathematics to collaborate, build, design, analyze, and share graph network models. As free software that's notable for its scalability and portability, NetworkX has been widely adopted by Python enthusiasts.


2 Answers

You have two easy options:

Up the DPI

plt.savefig("graph.png", dpi=1000) 

(larger image file size)

Save as a PDF

plt.savefig("graph.pdf") 

This is the best option, as the final graph is not rasterized. In theory, you should be able to zoom in indefinitely.

like image 79
Hooked Avatar answered Sep 30 '22 03:09

Hooked


While not in GTK, you might want to check out NetworkX Viewer.

like image 26
jsexauer Avatar answered Sep 30 '22 01:09

jsexauer