Drawing a huge graph with networkX and matplotlib

Nihar Sarangi picture Nihar Sarangi · Feb 22, 2012 · Viewed 25.7k times · Source

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) 

Answer

Hooked picture Hooked · Feb 23, 2012

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.