Drawing a huge graph with networkX and matplotlib

2019-03-17 23:14发布

问题:

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) 

回答1:

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.



回答2:

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