0

I have a very complicated graph that includes lots of nodes and edges. For better understanding, I want to visualize the graph. I worked with lots of libraries and I didn't get what I want.

This article was great, but I couldn't define the pos attribute for my graph. The random geometric graph has a pos attribute by default.

number of nodes = 567
number of edges = 846

I used nx.random_layout() to define 'pos' attributes for my nodes. My graph name is taradodGraph

pos = nx.random_layout(taradodGraph)
dmin = 1
ncenter = 0
for n in pos:
    x, y = pos[n]
    d = (x - 0.5)**2 + (y - 0.5)**2
    if d < dmin:
        ncenter = n
        dmin = d

# color by path length from node near center
p = dict(nx.single_source_shortest_path_length(taradodGraph, ncenter))

plt.figure(figsize=(8, 8))
nx.draw_networkx_edges(taradodGraph, pos, nodelist=[ncenter], alpha=0.4)
nx.draw_networkx_nodes(taradodGraph, pos, nodelist=list(p.keys()),
                       node_size=80,
                       node_color=list(p.values()),
                       cmap=plt.cm.Reds_r)

plt.xlim(-0.05, 1.05)
plt.ylim(-0.05, 1.05)
plt.axis('off')
plt.savefig('foo.png')
plt.show()

Best of the trials: a trial graph output

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • 3
    What have you tried so far? – aliaksei Nov 23 '19 at 13:25
  • 2
    Please add some more details: number of nodes, number of edges, typical number of nodes connected to one, ... . If possible, also create an example of a graph. Or at least provide the best image you got while investigating. – JohanC Nov 23 '19 at 14:17
  • 2
    See https://networkx.github.io/documentation/networkx-1.11/reference/drawing.html#module-networkx.drawing.layout for how to calculate the positions. Typically a spring layout gives the best results. – JohanC Nov 23 '19 at 14:24
  • 3
    Apart from Python, there is the javascript library d3.js that can create an interactive graph layout. Forces are applied to keep nodes at an ideal distance. If you dive deep into d3js, you can also play with the priorities that govern these forces (e.g. do edges have an equal weight, do you want a lot of clustering, ...). See e.g. https://ipython-books.github.io/64-visualizing-a-networkx-graph-in-the-notebook-with-d3js/ for how to connect python and d3js. – JohanC Nov 23 '19 at 14:34
  • 2
    A random layout only makes sense when there are very little nodes. With chaos, it is hard to see order. Best to experiment trying out many layouts. Have a look at https://github.com/d3/d3/wiki/Gallery to get a feel and try to find a layout that is close to your situation. – JohanC Nov 23 '19 at 15:52
  • I added more details @aliaksei – Sayvan Kalami Nov 23 '19 at 16:01
  • I will try what you suggested. thank you @JohanC – Sayvan Kalami Nov 23 '19 at 16:28
  • 1
    With about the same number of edges as nodes, you absolutely need to try some kind of force layout. – JohanC Nov 23 '19 at 16:45
  • With 567 nodes, it would likely help if you left off the node labels. – G5W Nov 25 '19 at 00:29

0 Answers0