Question

I am trying to plot a graph from a distance matrix. The code works fine and gives me an image. The image is big but all the nodes are packed together. I want increase the space between the nodes. I want the nodes to be more apart not enlarged.

I tried Graphviz NEATO, But the problem with it is that it supports only 100 nodes, Since I have 1000 nodes, it was showing 10 clusters of 100 nodes each.

My overall code -

import networkx as nx
import pickle
import matplotlib.pyplot as plt
print "~~~Unpickle."
p_file = open('pickles/names')
Names = pickle.load(p_file)
p_file.close()
p_file = open('pickles/distance')
Dist = pickle.load(p_file)
p_file.close()

G = nx.Graph()

print "~~~Inserting Nodes."
for store in Names:
    G.add_node(store)

print "~~~Inserting Edges."

for i in range(601):
    for j in range(601):
        if Names[i]!=Names[j]:
            G.add_edge(Names[i],Names[j],weight=Dist[i][j])
print "~~~Drawing Graph."
nx.draw(G,pos,node_color='#A0CBE2',edge_color='none',width=1, edge_cmap=plt.cm.Blues, with_labels=False)
print "~~~Saving Figure."
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) 
print "~~~Success!"

Output (edges and labels removed) : enter image description here

Output of Graphvix NEATO with 600 Nodes -

enter image description here

Was it helpful?

Solution

From figure documentation:

figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')

So figure(figsize=(10,10)) creates an 10 inch -by- 10 inch image, which will be 800-by-800 pixels.

If you want to print a large network graph, the easiest way is to increase DPI.

Try something like:

plt.figure(num=None, figsize=(10, 10), dpi=1200). This will produce a large image file.

You can also try printing pdf

plt.savefig("graph.pdf")

Using this option, the final graph is will not be rasterized.

To change the distance between nodes you can make the nodes smaller or change the layout. Changing the layout is hard to do well.

Try the scale option in layout for example,

scale = 2

will double distance between all nodes.

So in your example, change the line

nx.draw(G)

to

pos = nx.circular_layout(G, scale=2)
nx.draw(G, pos)

You can also use different layouts see documentation for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top