How to improve network graph visualization? [closed]
Asked Answered
S

2

15

I tried to use networkx in python to turn an adjacent matrix into a graph. My "weighted" graph has about 8000 nodes and 14000 edges. Is there a great layout form or other packages, tools to make my graph become more beautiful? I hope the outcome is that the edge weight higher the nodes become closer. So that I could analyze the cluster nodes.

I had tried all the layout provided in networkx document. I also tried to use gephi and it still a little bit not satisfied with my ideal. This is how it look in networkx. It can show out all the cluster but it looks a little bit terrible for someone who has Intensive phobia:

enter image description here

Here is the code I used to draw the graph:

G = nx.Graph()
for i in range(10000):
    for j in range(10000):
        if mat[i][j] > 10:
            G.add_edge(a[i], a[j], weight=mat[i][j])
pos = nx.nx_pydot.graphviz_layout(G)
plt.figure(figsize=(8, 8))
plt.axis('off')
nx.draw_networkx(G, pos=pos, with_labels=False, node_size=25, edgecolors='black', edge_color='b')
plt.show()
Slivovitz answered 22/8, 2019 at 6:23 Comment(0)
B
28

Let's dive a bit into large graph visualization:

In the context of your question you have three ways to visualize a graph:

  1. Draw the whole graph on the one screen
  2. Draw the graph on the surface that is larger than one screen
  3. Partially draw the graph or reduce the number of drawn elements

We will review these ways one by one:

1. Compress the incompressible

You have 8000 nodes and 14000 edges in your graph. Let's imagine that you only need to draw nodes without edges. So for an average FullHD display you will have:

1920 * 1080 / 8000 = 259 pixels per node. It is:

sqrt(259) = 17

A 17×17 px. node if you will tile the whole display with nodes. If you want to draw node labels, you will have:

17 - 1 * 2 (outer node borders) - 1 * 2 (inner node borders) = 13×13 square. The smallest possible font (I will not talk about esoteric fonts) has 3×3(+1) size so you can store no more than 9 characters per node.

It looks like this:

enter image description here

And we still haven't drawn graph edges! If we want to do it, we will use:

1920 * 1080 / (8000 + 14000) = 94 pixels per graph element, and:

sqrt(94) = 9.7 - 9×9 px. nodes so it is nearly impossible to have even simple labels on them.

I think it is obvious now that the whole graph on the single screen will always be looking like a horrifying mess. So it's not an option to draw your graph this way.

2. Expand the nonexpandable

If we can't draw the graph on one screen, we can rake our brains and invent a way out - to draw it on several screens! We can do it two ways:

  • Using matplotlib
  • Using graphviz

In matplotlib case we create a very large figure (with figsize argument) and then import our graph to a picture (with plt.savefig):

import networkx as nx 
import matplotlib.pyplot as plt 

fig = plt.figure(figsize=(40, 40)) 
G = nx.fast_gnp_random_graph(300, 0.02, seed=1337) 
nx.draw(G, node_size=30) 
plt.axis('equal') 
plt.show() 
fig.savefig('waka.svg') 

So we will have a big vector picture (here is a small part of it):

enter image description here

In this case we can use any graph layout available in networkx.

Another way is to use Graphviz external library that will draw our graph. Honestly, in this case networkx will just convert the graph to .dot file and send it to Graphviz. The main disadvantage is that you can't control how Graphviz will draw your graph. But it has pretty good layouts so the result will be acceptable.

Use this Python code to generate .dot file:

import networkx as nx 

G = nx.fast_gnp_random_graph(300, 0.02, seed=1337) 
nx.nx_agraph.write_dot(G, './waka.dot')

and then use Graphviz:

dot -Tsvg waka.dot >waka.svg

or for non-directed graphs:

neato -Tsvg waka.dot -Goverlap=false >waka.svg

so the result picture will be looking like this (here is a small part of the whole picture too):

enter image description here

Graphviz has the huge amount of various options so you can transform your graph looking nearly as you wish.

3. Cut the uncuttable

In your graph you have something like a giant connected component ( GCC ) and many small subgraphs. The whole picture is about 580×580 size and GCC is about 290×290 size so GCC uses:

(290 × 290) ÷ (580 × 580) × 100 = 25% of your canvas. If you will keep only GCC, you will have 4 times more space to draw! In this case you can draw another connected components on another image.

You can keep only GCC with code like this:

import networkx as nx 

G = nx.fast_gnp_random_graph(300, 0.01, seed=1337)
gcc = max(nx.connected_components(G), key=lambda x: len(x))
H = G.subgraph(gcc)
nx.draw(G, node_size=30, node_color='red')

will draw the original graph:

enter image description here

And with this last line:

nx.draw(H, node_size=30, node_color='red')

it will draw only the GCC:

enter image description here


P.S.

What else you can try to do with your graph:

  • Clusterize nodes
  • Play with node size (PageRank etc)
  • Play with edge color
  • Don't draw edges but keep the layout
  • WEEP (in case my answer didn't help you even a bit)
Backstroke answered 28/8, 2019 at 17:55 Comment(2)
I'm very grateful for your answer. Will those layouts in networkx help me to put nodes together if the weight of the edge between them is higher? (i.e. the weight of edge AB is 10, the weight of BC is 3, then the length of edge AB will less than the length of BC) Or something I need to study to deal with the case?Slivovitz
Yes, with matplotlib visualization. In Graphviz case it will draw nodes by itself.Backstroke
G
4

Another way to improve visualisation is to plot in 3D. This helps disambiguate areas of connectivity as there is an extra degree of freedom. A tool that can do this is Graphia. Disclaimer: I am involved in the development of this application.

Graphia Main Window

Glycerin answered 9/6, 2020 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.