#322 Network layout possibilities

Chart #320 and #321 explain how to realise a basic network chart and custom its appearance. The next step is to control the layout of your network. There is actually an algorithm that calculate the most optimal position of each node. Several algorithm have been developed and are proposed by NetworkX. This page illustrate this concept by taking the same small dataset and applying different layout algorithm on it. If you have no idea which one is the best for you, just let it by default! (It will be the fruchterman Reingold solution). Read more about it with help(nx.layout).


# libraries
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

# Build a dataframe with your connections
df = pd.DataFrame({ 'from':['A', 'B', 'C','A','E','F','E','G','G','D','F'], 'to':['D', 'A', 'E','C','A','F','G','D','B','G','C']})
df

# Build your graph
G=nx.from_pandas_dataframe(df, 'from', 'to')

# Fruchterman Reingold
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.fruchterman_reingold_layout(G))
plt.title("fruchterman_reingold")

# Circular
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.circular_layout(G))
plt.title("circular")

# Random
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.random_layout(G))
plt.title("random")

# Spectral
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.spectral_layout(G))
plt.title("spectral")

# Spring
nx.draw(G, with_labels=True, node_size=1500, node_color="skyblue", pos=nx.spring_layout(G))
plt.title("spring")

Leave a Reply

Your email address will not be published.